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

查看:226
本文介绍了Django - 如何使用南方重命名模型字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

使用南方做最简单的方法是什么? / p>

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自动从您的模型类名称和包含它的应用程序中导出数据库表的名称。模型的数据库表名称是通过将模型的app label(您在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 (即,即使它们是骆驼,也不会在项目项目之间插入下划线)

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 - 如何使用南方重命名模型字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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