且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于重命名模型和关系字段的 Django 迁移策略

更新时间:2023-12-01 22:15:34

所以当我尝试这个的时候,你似乎可以压缩步骤 3 - 7:

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')
    ]

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

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 (!).

更新:作为 ceasaro 提到,较新版本的 Django 通常能够检测并询问模型是否已重命名.所以先试试 manage.py makemigrations 再检查迁移文件.

Update: As ceasaro mentions, newer versions of Django are usually able to detect and ask if a model is renamed. So try manage.py makemigrations first and then check the migration file.