Django migration strategy for renaming a model and relationship fields

后端 未结 12 1613
一个人的身影
一个人的身影 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条回答
  •  误落风尘
    2020-11-28 01:49

    At first, I thought that Fiver's method worked for me because the migration worked well until step 4. However, the implicit changes 'ForeignKeyField(Foo)' into 'ForeignKeyField(Bar)' was not related in any migrations. This is why migration failed when I wanted to rename relationship fields (step 5-8). This might be due to the fact that my 'AnotherModel' and 'YetAnotherModel' are dispatched in other apps in my case.

    So I managed to rename my models and relationship fields doing following below steps:

    I adapted the method from this and particularly the trick of otranzer.

    So like Fiver let's say we have in myapp:

    class Foo(models.Model):
        name = models.CharField(unique=True, max_length=32)
        description = models.TextField(null=True, blank=True)
    

    And in myotherapp:

    class AnotherModel(models.Model):
        foo = models.ForeignKey(Foo)
        is_awesome = models.BooleanField()
    
    
    class YetAnotherModel(models.Model):
        foo = models.ForeignKey(Foo)
        is_ridonkulous = models.BooleanField()
    

    Step 1:

    Transform every OneToOneField(Foo) or ForeignKeyField(Foo) into IntegerField(). (This will keep the id of related Foo object as value of the integerfield).

    class AnotherModel(models.Model):
        foo = models.IntegerField()
        is_awesome = models.BooleanField()
    
    class YetAnotherModel(models.Model):
        foo = models.IntegerField()
        is_ridonkulous = models.BooleanField()
    

    Then

    python manage.py makemigrations
    
    python manage.py migrate
    

    Step 2: (Like step 2-4 from Fiver)

    Change the model name

    class Bar(models.Model):  # <-- changed model name
        name = models.CharField(unique=True, max_length=32)
        description = models.TextField(null=True, blank=True)
    

    Create an empty migration:

    python manage.py makemigrations --empty myapp
    

    Then edit it like:

    class Migration(migrations.Migration):
    
        dependencies = [
            ('myapp', '0001_initial'),
        ]
    
        operations = [
            migrations.RenameModel('Foo', 'Bar')
        ]
    

    Eventually

    python manage.py migrate
    

    Step 3:

    Transform Back your IntegerField() into their previous ForeignKeyField or OneToOneField but with the new Bar Model. (The previous integerfield was storing the id, so django understand that and reestablish the connection, which is cool.)

    class AnotherModel(models.Model):
        foo = models.ForeignKey(Bar)
        is_awesome = models.BooleanField()
    
    class YetAnotherModel(models.Model):
        foo = models.ForeignKey(Bar)
        is_ridonkulous = models.BooleanField()
    

    Then do:

    python manage.py makemigrations 
    

    Very importantly, at this step you have to modify every new migrations and add the dependency on the RenameModel Foo-> Bar migrations. So if both AnotherModel and YetAnotherModel are in myotherapp the created migration in myotherapp must look like this:

    class Migration(migrations.Migration):
    
        dependencies = [
            ('myapp', '00XX_the_migration_of_myapp_with_renamemodel_foo_bar'),
            ('myotherapp', '00xx_the_migration_of_myotherapp_with_integerfield'),
        ]
    
        operations = [
            migrations.AlterField(
                model_name='anothermodel',
                name='foo',
                field=models.ForeignKey(to='myapp.Bar'),
            ),
            migrations.AlterField(
                model_name='yetanothermodel',
                name='foo',
                field=models.ForeignKey(to='myapp.Bar')
            ),
        ]
    

    Then

    python manage.py migrate
    

    Step 4:

    Eventually you can rename your fields

    class AnotherModel(models.Model):
        bar = models.ForeignKey(Bar) <------- Renamed fields
        is_awesome = models.BooleanField()
    
    
    class YetAnotherModel(models.Model):
        bar = models.ForeignKey(Bar) <------- Renamed fields
        is_ridonkulous = models.BooleanField()
    

    and then do automatic renaming

    python manage.py makemigrations
    

    (django should ask you if you actually renamed the modelname, say yes)

    python manage.py migrate
    

    And that's it!

    This works on Django1.8

提交回复
热议问题