Django migration strategy for renaming a model and relationship fields

后端 未结 12 1616
一个人的身影
一个人的身影 2020-11-28 00:54

I\'m planning to rename several models in an existing Django project where there are many other models that have foreign key relationships to the models I would like to rena

12条回答
  •  猫巷女王i
    2020-11-28 01:53

    I needed to do the same thing and follow. I changed the model all at once (Steps 1 and 5 together from Fiver's answer). Then created a schema migration but edited it to be this:

    class Migration(SchemaMigration):
        def forwards(self, orm):
            db.rename_table('Foo','Bar')
    
        def backwards(self, orm):
            db.rename_table('Bar','Foo')
    

    This worked perfectly. All my existing data showed up, all the other tables referenced Bar fine.

    from here: https://hanmir.wordpress.com/2012/08/30/rename-model-django-south-migration/

提交回复
热议问题