首页 文章

Django - 如何使用South重命名模型字段?

提问于
浏览
205

我想更改模型中特定字段的名称:

class Foo(models.Model):
    name = models.CharField()
    rel  = models.ForeignKey(Bar)

应改为:

class Foo(models.Model):
    full_name     = models.CharField()
    odd_relation  = models.ForeignKey(Bar)

使用南方最简单的方法是什么?

6 回答

  • 8

    Just change the model and run makemigrations in 1.9

    Django会自动检测到您已删除并创建了一个字段,并询问:

    Did you rename model.old to model.new (a IntegerField)? [y/N]
    

    说是,并且创建了正确的迁移 . 魔法 .

  • 228
    • 在项目设置文件中将 south 添加到已安装的应用程序中 .

    • 注释掉添加/修改的字段/表 .

    • $ manage.py Schemamigration <app_name> --initial

    • $ manage.py migrate <app_name> --Fake

    • 取消注释该字段并写入修改后的字段

    • $ manage.py Schemamigration --auto

    • $ manage.py migrate <app_name>

    如果您使用的是'pycharm',那么您可以使用'ctrl shift r'代替'manage.py',并使用'shift'作为参数 .

  • 0

    这是我做的:

    • 在模型中更改列名称(在此示例中为 myapp/models.py

    • 运行 ./manage.py schemamigration myapp renaming_column_x --auto

    注意 renaming_column_x 可以是您喜欢的任何内容,它只是为迁移文件提供描述性名称的一种方式 .

    这将生成一个名为 myapp/migrations/000x_renaming_column_x.py 的文件,该文件将删除旧列并添加新列 .

    修改此文件中的代码以将迁移行为更改为简单重命名:

    class Migration(SchemaMigration):
    
        def forwards(self, orm):
            # Renaming column 'mymodel.old_column_name' to 'mymodel.new_column_name'
            db.rename_column(u'myapp_mymodel', 'old_column_name', 'new_column_name')
    
        def backwards(self, orm):
            # Renaming column 'mymodel.new_column_name' to 'mymodel.old_column_name'
            db.rename_column(u'myapp_mymodel', 'new_column_name', 'old_column_name')
    
  • 15

    您可以使用db.rename_column功能 .

    class Migration:
    
        def forwards(self, orm):
            # Rename 'name' field to 'full_name'
            db.rename_column('app_foo', 'name', 'full_name')
    
    
    
    
        def backwards(self, orm):
            # Rename 'full_name' field to 'name'
            db.rename_column('app_foo', 'full_name', 'name')
    

    db.rename_column 的第一个参数是表名,所以记住Django creates table names是多么重要:

    Django自动从模型类的名称和包含它的应用程序派生数据库表的名称 . 模型的数据库表名称是通过将模型的“app label”(您在manage.py startapp中使用的名称)连接到模型的类名称,并在它们之间加下划线来构建的 .

    如果你有一个多措辞,驼峰式的模型名称,例如ProjectItem,表名将是 app_projectitem (即,即使它们是驼峰式的,也不会在 project 和_344798之间插入下划线) .

  • 39

    我不知道db.rename列,听起来很方便,但是在过去我添加了新列作为一个schemamigration,然后创建了一个数据迁移,将值移动到新字段,然后第二个schemamigration删除旧列

  • 5

    Django 1.7引入了Migrations所以现在你甚至不需要安装额外的软件包来管理你的迁移 .

    要重命名模型,您需要先创建空迁移:

    $ manage.py makemigrations <app_name> --empty
    

    然后,您需要编辑迁移的代码,如下所示:

    from django.db import models, migrations
    
    class Migration(migrations.Migration):
    
    dependencies = [
        ('yourapp', 'XXXX_your_previous_migration'),
    ]
    
    operations = [
        migrations.RenameField(
            model_name='Foo',
            old_name='name',
            new_name='full_name'
        ),
        migrations.RenameField(
            model_name='Foo',
            old_name='rel',
            new_name='odd_relation'
        ),
    ]
    

    之后你需要运行:

    $ manage.py migrate <app_name>
    

相关问题