使用 Django/South 重命名模型的最简单方法? [英] Easiest way to rename a model using Django/South?
问题描述
我一直在 South 的网站、Google 和 SO 上寻找答案,但找不到一种简单的方法来做到这一点.
我想使用 South 重命名 Django 模型.假设您有以下内容:
class Foo(models.Model):名称 = 模型.CharField()类 FooTwo(models.Model):名称 = 模型.CharField()foo = models.ForeignKey(Foo)
并且您想将 Foo 转换为 Bar,即
class Bar(models.Model):名称 = 模型.CharField()类 FooTwo(models.Model):名称 = 模型.CharField()foo = models.ForeignKey(Bar)
为了简单起见,我只是尝试将名称从 Foo
更改为 Bar
,但忽略 foo
中的成员 foo
code>FooTwo 现在.
使用 South 最简单的方法是什么?
- 我可能会进行数据迁移,但这似乎很复杂.
- 编写自定义迁移,例如
db.rename_table('city_citystate', 'geo_citystate')
,但我不确定在这种情况下如何修复外键. - 您知道一种更简单的方法吗?
要回答您的第一个问题,简单的模型/表重命名非常简单.运行命令:
./manage.py schemamigration yourapp rename_foo_to_bar --empty
(更新 2:尝试 --auto
而不是 --empty
以避免下面的警告.感谢@KFB 的提示.)
如果您使用的是旧版本的 south,则需要 startmigration
而不是 schemamigration
.
然后手动编辑迁移文件,如下所示:
类迁移(SchemaMigration):def 转发(自我,orm):db.rename_table('yourapp_foo', 'yourapp_bar')向后定义(自我,orm):db.rename_table('yourapp_bar','yourapp_foo')
您可以使用模型类中的 db_table
Meta 选项更简单地完成此操作.但是每次这样做时,都会增加代码库的遗留权重——类名与表名不同会使您的代码更难理解和维护.为了清楚起见,我完全支持像这样进行简单的重构.
(更新)我刚刚在生产中尝试过这个,当我去应用迁移时收到一个奇怪的警告.它说:
<块引用>以下内容类型已过时,需要删除:您的应用 |富通过外键与这些内容类型相关的任何对象也将被删除.您确定要删除这些内容类型吗?如果您不确定,请回答否".
我回答没有",一切似乎都很好.
I've been hunting for an answer to this on South's site, Google, and SO, but couldn't find a simple way to do this.
I want to rename a Django model using South. Say you have the following:
class Foo(models.Model):
name = models.CharField()
class FooTwo(models.Model):
name = models.CharField()
foo = models.ForeignKey(Foo)
and you want to convert Foo to Bar, namely
class Bar(models.Model):
name = models.CharField()
class FooTwo(models.Model):
name = models.CharField()
foo = models.ForeignKey(Bar)
To keep it simple, I'm just trying to change the name from Foo
to Bar
, but ignore the foo
member in FooTwo
for now.
What's the easiest way to do this using South?
- I could probably do a data migration, but that seems pretty involved.
- Write a custom migration, e.g.
db.rename_table('city_citystate', 'geo_citystate')
, but I'm not sure how to fix the foreign key in this case. - An easier way that you know?
To answer your first question, the simple model/table rename is pretty straightforward. Run the command:
./manage.py schemamigration yourapp rename_foo_to_bar --empty
(Update 2: try --auto
instead of --empty
to avoid the warning below. Thanks to @KFB for the tip.)
If you're using an older version of south, you'll need startmigration
instead of schemamigration
.
Then manually edit the migration file to look like this:
class Migration(SchemaMigration):
def forwards(self, orm):
db.rename_table('yourapp_foo', 'yourapp_bar')
def backwards(self, orm):
db.rename_table('yourapp_bar','yourapp_foo')
You can accomplish this more simply using the db_table
Meta option in your model class. But every time you do that, you increase the legacy weight of your codebase -- having class names differ from table names makes your code harder to understand and maintain. I fully support doing simple refactorings like this for the sake of clarity.
(update) I just tried this in production, and got a strange warning when I went to apply the migration. It said:
The following content types are stale and need to be deleted: yourapp | foo Any objects related to these content types by a foreign key will also be deleted. Are you sure you want to delete these content types? If you're unsure, answer 'no'.
I answered "no" and everything seemed to be fine.
这篇关于使用 Django/South 重命名模型的最简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!