我计划在现有 Django 项目中重命名多个模型,其中有许多其他模型与我要重命名的模型具有外键关系。我相当肯定这将需要多次迁移,但我不确定具体的过程。
假设我从名为 myapp
的 Django 应用程序中的以下模型开始:
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()
我想重命名 Foo
模型,因为该名称实际上没有意义并且会导致代码困惑,而 Bar
会使名称更清晰.
根据我在 Django 开发文档中阅读的内容,我假设以下迁移策略:
修改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()
注意 foo
的 AnotherModel
字段名称不会改变,但关系会更新为 Bar
模型。我的理由是我不应该一次更改太多,如果我将此字段名称更改为 bar
我可能会丢失该列中的数据。
创建一个空迁移:
python manage.py makemigrations --empty myapp
编辑步骤2中创建的迁移文件中的Migration
类,将RenameModel
操作添加到操作列表中:
class Migration(migrations.Migration):
dependencies = [
('myapp', '0001_initial'),
]
operations = [
migrations.RenameModel('Foo', 'Bar')
]
应用迁移:
python manage.py migrate
编辑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()
创建另一个空迁移:
python manage.py makemigrations --empty myapp
编辑步骤 6 中创建的迁移文件中的 Migration
类,将任何相关字段名称的 RenameField
操作添加到操作列表中:
class Migration(migrations.Migration):
dependencies = [
('myapp', '0002_rename_fields'), # <-- is this okay?
]
operations = [
migrations.RenameField('AnotherModel', 'foo', 'bar'),
migrations.RenameField('YetAnotherModel', 'foo', 'bar')
]
应用第二次迁移:
python manage.py migrate
除了更新其余代码( View 、表单等)以反射(reflect)新的变量名称之外,这基本上是新迁移功能的工作方式吗?
此外,这似乎需要很多步骤。迁移操作能否以某种方式压缩?
谢谢!
最佳答案
所以当我尝试这个时,似乎你可以压缩第 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')
]
如果您不更新导入它的名称,例如,您可能会遇到一些错误。 admin.py 甚至更旧的迁移文件 (!)。
更新:作为ceasaro提到,较新版本的 Django 通常能够检测并询问模型是否被重命名。所以先试试 manage.py makemigrations
然后检查迁移文件。
https://stackoverflow.com/questions/25091130/