更新时间:2023-12-01 22:10:28
您可以使用 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).