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?