Finally, after a while I got a procedure with django-south , which may help others. The key was in the south depends_on (http://south.aeracode.org/wiki/Dependencies). I did this in 4 steps:
First
- Create a placeholder for foreign key values ββin model
A
So model A becomes:
class A(models.Model): instance_b_placeholder = models.ForeignKey(A, null=True, blank=True)
Now just run manage.py schemamigration app1 --auto .
Second
- Create a datamigration so that we can copy the values. The goal is to duplicate the data in db and then rename the attributes and delete the old ones. Error
manage.py datamigration app1 update_fields . I decided to keep datamigration in app1 . If you do not, just make sure it starts after the previous migration.
The data encoding is encoded here:
# Forwards: for b in orm['app2.B'].objects.filter(instance_b__isnull=False): b.instance_a.instance_b_placeholder = b b.instance_a.save()
Third
- Remove the
instance_b field from model B and be sure to make a migration run after the one created in the previous step.
Model B becomes:
class B(models.Model):
The problem is manage.py schemamigration app2 --auto and edit the migration by adding the previous migration to depends_on :
depends_on = ( ("app1", "<migration_number>_update_fields"), )
Fourth step :
Rename the ownerβs place. This is achieved by changing the name in the code and editing the migration. Editing is necessary since south tends to delete and add a new column, but we only want it to rename the column.
This migration should work in last place, so I made it dependent on the previous one.
Here is the code:
depends_on = ( ("app2", "<previous_migration_here>"), ) # Forwards: db.rename_column('app1_a', 'instance_b_placeholder_id', 'instance_b_id') # Backwards: db.rename_column('app1_a', 'instance_b_id', 'instance_b_placeholder_id')
So what is it. I donβt know if there are so many other ways to do this, but at least it helped me.
Fred source share