[Answered ]-Django – How to move attribute to another model and delete it

2👍

That’s why you get the apps argument to the data migration. It holds definitions of the models as they were when you created the data migration.

So do

def move_info_to_class_d(apps, schema_editor):
    C = apps.get_model('yourappname', 'C')
    D = apps.get_model('yourappname', 'D')

    objs = C.objects.all()

    for obj in objs.iterator():
        d = D.objects.create(y2=obj.y)

And it will work fine (as long as you create the migration that removes C.y after this one).

Edit: oh, and objs.iterator() is unnecessary, objs is already iterable by itself.

Leave a comment