Django - 如何使用 South 重命名模型字段? [英] Django - How to rename a model field using South?

查看:25
本文介绍了Django - 如何使用 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)

应该改为:

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

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

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

推荐答案

您可以使用 db.rename_column 函数.

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

db.rename_column 的第一个参数是表名,所以重要的是要记住 Django 创建表名:

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

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

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.

如果您有一个多词、驼峰式的模型名称,例如 ProjectItem,则表名将是 app_projectitem(即,不会在 之间插入下划线projectitem 即使它们是驼峰式的.

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

这篇关于Django - 如何使用 South 重命名模型字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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