gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:14:55 25 4
gpt4 key购买 nike

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

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 最简单的方法是什么?

最佳答案

您可以使用 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 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 即使它们是驼峰式的)。

关于python - Django - 如何使用 South 重命名模型字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3235995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com