重命名模型和关系领域的Django迁移策略 [英] Django migration strategy for renaming a model and relationship fields

查看:142
本文介绍了重命名模型和关系领域的Django迁移策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划在现有Django项目中重命名几个模型,其中还有许多其他模型与我想重命名的模型有外键关系。我相当确定这将需要多次迁移,但我不确定确切的程序。



让我们来看看在Django应用程序中的以下模型称为 myapp

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


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


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

I想要重命名 Foo 模型,因为这个名字没有真正的意义,导致代码混乱,而 Bar

根据我在Django开发文档中阅读的内容,我承担以下迁移策略:



步骤1



修改 models.py

  class Bar(models.Model):#<  - 更改模型名称
name = models.CharField(unique = True,max_length = 32)
description = models .TextField(null = True,blank = True)


class AnotherModel(models.Model):
foo = models.ForeignKey(Bar)#< ,但不是字段名称
is_awesome = models.BooleanField()


class YetAnotherModel(models.Model):
foo = models.ForeignKey(Bar)#< ; - 改变了关系,但不是字段名称
is_ridonkulous = models.BooleanField()

请注意, foo AnotherModel 字段名称不会更改,但关系更新为 Bar 模型。我的推理是,我不应该一次更改太多,如果我将此字段名称更改为 bar ,那么我可能会丢失该列中的数据。



步骤2



创建一个空迁移:

  python manage.py makemigrations --empty myapp 



步骤3



在步骤2中创建的迁移文件中编辑 Migration 类以添加 RenameModel 操作到操作列表:

  class Migration(migrations.Migration):

依赖关系= [
('myapp','0001_initial'),
]

operations = [
migrations.RenameModel('Foo','Bar')
]



步骤4



应用迁移:

  python manage.py migrate 



步骤5



models.py

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


class AnotherModel .Model):
bar = models.ForeignKey(Bar)#< - 更改的字段名称
is_awesome = models.BooleanField()


class YetAnotherModel model.Model):
bar = models.ForeignKey(Bar)#< - 更改的字段名称
is_ridonkulous = models.BooleanField()



步骤6



创建另一个空迁移:

  python manage.py makemigrations --empty myapp 



步骤7



在步骤6中创建的迁移文件中编辑 Migration 类以添加 RenameField 对操作列表的任何相关字段名称的操作:

  class Migration 。移民) :

依赖关系= [
('myapp','0002_rename_fields'),#< - 可以吗?
]

operations = [
migrations.RenameField('AnotherModel','foo','bar'),
migrations.RenameField('YetAnotherModel' foo','bar')
]



步骤8



应用第二次迁移:

  python manage.py migrate 






除了将其余的代码(视图,表单等)更新为反映新的变量名称,这基本上如何新的迁移功能将会起作用?



此外,这似乎是很多步骤。可以以某种方式缩小迁移操作吗?



谢谢!

解决方案

p>所以当我尝试这个,似乎你可以缩小步骤3 - 7:

 类Migration(migrations.Migration): 

依赖关系= [
('myapp','0001_initial'),
]

operations = [
migrations.RenameModel(' Foo','Bar'),
migrations.RenameField('AnotherModel','foo','bar'),
migrations.RenameField('YetAnotherModel','foo','bar')
]

如果您不更新其导入的名称,可能会收到一些错误admin.py甚至更旧的迁移文件(!)


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 rename. I'm fairly certain this will require multiple migrations, but I'm not sure of the exact procedure.

Let's say I start out with the following models within a Django app called myapp:

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


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


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

I want to rename the Foo model because the name doesn't really make sense and is causing confusion in the code, and Bar would make for a much clearer name.

From what I have read in the Django development documentation, I'm assuming the following migration strategy:

Step 1

Modify models.py:

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


class AnotherModel(models.Model):
    foo = models.ForeignKey(Bar)  # <-- changed relation, but not field name
    is_awesome = models.BooleanField()


class YetAnotherModel(models.Model):
    foo = models.ForeignKey(Bar)  # <-- changed relation, but not field name
    is_ridonkulous = models.BooleanField()

Note the AnotherModel field name for foo doesn't change, but the relation is updated to the Bar model. My reasoning is that I shouldn't change too much at once and that if I changed this field name to bar I would risk losing the data in that column.

Step 2

Create an empty migration:

python manage.py makemigrations --empty myapp

Step 3

Edit the Migration class in the migration file created in step 2 to add the RenameModel operation to the operations list:

class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0001_initial'),
    ]

    operations = [
        migrations.RenameModel('Foo', 'Bar')
    ]

Step 4

Apply the migration:

python manage.py migrate

Step 5

Edit the related field names in models.py:

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


class AnotherModel(models.Model):
    bar = models.ForeignKey(Bar)  # <-- changed field name
    is_awesome = models.BooleanField()


class YetAnotherModel(models.Model):
    bar = models.ForeignKey(Bar)  # <-- changed field name
    is_ridonkulous = models.BooleanField()

Step 6

Create another empty migration:

python manage.py makemigrations --empty myapp

Step 7

Edit the Migration class in the migration file created in step 6 to add the RenameField operation(s) for any related field names to the operations list:

class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0002_rename_fields'),  # <-- is this okay?
    ]

    operations = [
        migrations.RenameField('AnotherModel', 'foo', 'bar'),
        migrations.RenameField('YetAnotherModel', 'foo', 'bar')
    ]

Step 8

Apply the 2nd migration:

python manage.py migrate


Aside from updating the rest of the code (views, forms, etc.) to reflect the new variable names, is this basically how the new migration functionality would work?

Also, this seems like a lot of steps. Can the migration operations be condensed in some way?

Thanks!

解决方案

So when I tried this, it seems you can condense Step 3 - 7:

class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0001_initial'), 
    ]

    operations = [
        migrations.RenameModel('Foo', 'Bar'),
        migrations.RenameField('AnotherModel', 'foo', 'bar'),
        migrations.RenameField('YetAnotherModel', 'foo', 'bar')
    ]

You may get some errors if you don't update the names where it's imported e.g. admin.py and even older migration files (!)

这篇关于重命名模型和关系领域的Django迁移策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆