Django - 将多对多迁移到通过

Luk*_*rau 8 django manytomanyfield django-migrations

我有一个看起来像这样的模型:

class Assignment(models.Model):
    """An assignment covers a range of years, 
    has multiple coders and one specific task"""
    title = models.CharField(blank=True, max_length=100)
    start_date = models.DateField(default=date.today)
    end_date = models.DateField(default=date.today)
    coders = models.ManyToManyField(User, related_name='assignments')
Run Code Online (Sandbox Code Playgroud)

我想改变这个模型(它有数据已经​​在生产中),使它看起来像这样:

class Assignment(models.Model):
    """An assignment covers a range of years, 
    has multiple coders and one specific task"""
    title = models.CharField(blank=True, max_length=100)
    start_date = models.DateField(default=date.today)
    end_date = models.DateField(default=date.today)
    coders = models.ManyToManyField(User, through = 'AssignmentProgress')

class AssignmentProgress(models.Model):
    """The progress a particular coder has made with an assignment"""
    assignment = models.ForeignKey(Assignment, related_name="assignment_progress")
    coder = models.ForeignKey(User, related_name="assignment_progress")
    articles_coded = models.IntegerField(default=0, null=False)
    progress = models.FloatField(default=0, null=False)
Run Code Online (Sandbox Code Playgroud)

从我至今读,该解决方案将是一个字段添加assignment_coders = models.ManyToManyField(User, through = 'AssignmentProgress')Assignment,然后使用datamigration过度复制信息。

我尝试了以下数据迁移:

# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-11-09 16:31
from __future__ import unicode_literals

from django.db import migrations

def move_coders(apps, schema_editor):
    # We can't import the Person model directly as it may be a newer
    # version than this migration expects. We use the historical version.
    Assignment = apps.get_model("coding", "Assignment")
    for a in Assignment.objects.all():
        a.assignment_coders = a.coders
        a.save()


class Migration(migrations.Migration):

    dependencies = [
        ('coding', '0006_auto_20161109_1730'),
    ]

    operations = [
        migrations.RunPython(move_coders),
    ]
Run Code Online (Sandbox Code Playgroud)

但是当我尝试运行该迁移时出现以下错误: Cannot set values on a ManyToManyField which specifies an intermediary model.

如何在through=不丢失数据的情况下将 Assignment.coders 从多对多字段迁移到一个字段?