使用Django / South重命名模型的最简单的方法? [英] Easiest way to rename a model using Django/South?

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

问题描述

我在南方的网站Google和SO上一直在寻找答案,但找不到一个简单的方法。



我想使用South重命名Django模型。
说你有以下这些:

  class Foo(models.Model):
name = models。 CharField()

class FooTwo(models.Model):
name = models.CharField()
foo = models.ForeignKey(Foo)

并且您想将Foo转换为Bar,即

  class Bar(models.Model):
name = models.CharField()

class FooTwo(models.Model):
name = models.CharField )
foo = models.ForeignKey(Bar)

为了保持简单,我只是尝试将名称从 Foo 更改为 Bar ,但忽略 foo c $ c> fooTwo 中的成员。



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


  1. 我可能会做一个数据迁移,但这似乎很参与。

  2. 编写一个自定义迁移,例如 db.rename_table('city_citystate','geo_citystate'),但我不知道在这种情况下如何修复外键。



解决方案

为了回答你的第一个问题,简单的模型/表重命名非常简单。运行命令:

  ./ manage.py schemamigration yourapp rename_foo_to_bar --empty 

(更新2:尝试 - 自动而不是 - 空以避免下面的警告,感谢@KFB的提示。)



如果你使用的是旧的版本,需要 startmigration 而不是 schemamigration



然后手动编辑迁移文件如下所示:

  class Migration(SchemaMigration):

def forward self,orm):
db.rename_table('yourapp_foo','yourapp_bar')


def backwards(self,orm):
db.rename_table('你可以使用

您的模型类中的db_table 元选项。但每次你这样做,你可以增加代码库的遗留权重 - 类名与表名不同会使您的代码更难理解和维护。我完全支持这样做简单的重构,为了清楚起见。



(更新)我刚刚在生产中尝试过,当我去应用移民。它说:


 以下内容类型陈旧,需要删除:

yourapp | foo

通过外键与这些内容类型相关的任何对象也将删除
。您确定要删除这些内容类型吗?
如果您不确定,请回答否。


我回答不,一切似乎都很好/ p>

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?

  1. I could probably do a data migration, but that seems pretty involved.
  2. 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.
  3. 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屋!

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