Django - Migrate ManyToMany to through

人走茶凉 提交于 2019-12-14 04:18:00

问题


I have a model that looks like this:

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')

I want to change this model (which has data already in production) so that it looks like this:

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)

From what I have read so far, the solution would be to add a field assignment_coders = models.ManyToManyField(User, through = 'AssignmentProgress') to Assignment and then use a datamigration to copy the info over.

I tried that with the following data migration:

# -*- 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),
    ]

But I get the following error when I try to run that migration: Cannot set values on a ManyToManyField which specifies an intermediary model.

How can I migrate Assignment.coders from a ManyToMany field to one with through= without losing data?

来源:https://stackoverflow.com/questions/40512976/django-migrate-manytomany-to-through

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!