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

发布于 2024-09-08 21:48:02 字数 318 浏览 2 评论 0原文

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

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

应该更改为:

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

使用 South 执行此操作的最简单方法是什么?

I would like to change a name of specific fields in a model:

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

should change to:

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

What's the easiest way to do this using South?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

桃扇骨 2024-09-15 21:48:02

您可以使用 db.rename_column< /a> 函数。

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 创建表名称

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

如果您有一个多字、驼峰式命名的模型名称(例如 ProjectItem),则表名称将为 app_projectitem (即,在 project 和 item 即使它们是驼峰式的)。

You can use the db.rename_column function.

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

The first argument of db.rename_column is the table name, so it's important to remember how Django creates table names:

Django automatically derives the name of the database table from the name of your model class and the app that contains it. A model's database table name is constructed by joining the model's "app label" -- the name you used in manage.py startapp -- to the model's class name, with an underscore between them.

In the case where you have a multi-worded, camel-cased model name, such as ProjectItem, the table name will be app_projectitem (i.e., an underscore will not be inserted between project and item even though they are camel-cased).

有深☉意 2024-09-15 21:48:02

我的做法如下:

  1. 在模型中更改列名称(在本例中为 myapp/models.py
  2. 运行 ./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')

Here's what I do:

  1. Make the column name change in your model (in this example it would be myapp/models.py)
  2. Run ./manage.py schemamigration myapp renaming_column_x --auto

Note renaming_column_x can be anything you like, it's just a way of giving a descriptive name to the migration file.

This will generate you a file called myapp/migrations/000x_renaming_column_x.py which will delete your old column and add a new column.

Modify the code in this file to change the migration behaviour to a simple rename:

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')
自控 2024-09-15 21:48:02

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

I didn't know about db.rename column, sounds handy, however in the past I have added the new column as one schemamigration, then created a datamigration to move values into the new field, then a second schemamigration to remove the old column

浅笑依然 2024-09-15 21:48:02

Django 1.7 引入了迁移,因此现在您甚至不需要安装额外的包来管理迁移。

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

$ 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>

Django 1.7 introduced Migrations so now you don't even need to install extra package to manage your migrations.

To rename your model you need to create empty migration first:

$ manage.py makemigrations <app_name> --empty

Then you need to edit your migration's code like this:

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

And after that you need to run:

$ manage.py migrate <app_name>
少女七分熟 2024-09-15 21:48:02

只需更改模型并在 1.9 中运行 makemigrations

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

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

说“是”,就会创建正确的迁移。魔法。

Just change the model and run makemigrations in 1.9

Django automatically detects that you've deleted and created a single field, and asks:

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

Say yes, and the right migration gets created. Magic.

心如狂蝶 2024-09-15 21:48:02
  1. south 添加到项目设置文件中已安装的应用程序中。
  2. 注释掉添加/修改的字段/表。
  3. $ manage.py Schemamigration; --initial
  4. $ manage.py migrate; --Fake
  5. 取消该字段的注释并写入修改后的内容
  6. $ manage.py Schemamigration --auto
  7. $ manage.py migrate

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

  1. Add south to your installed apps in project setting file.
  2. Comment out the added/modified field/table.
  3. $ manage.py Schemamigration <app_name> --initial
  4. $ manage.py migrate <app_name> --Fake
  5. Un-comment the field and write the modified one
  6. $ manage.py Schemamigration --auto
  7. $ manage.py migrate <app_name>

If you are using 'pycharm', then you can use 'ctrl+shift+r' instead of 'manage.py' , and 'shift ' for parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文