使用Django和South重命名应用程序 [英] Renaming an app with Django and South

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

问题描述

我正在将应用程序重命名为更合适的名称。在这样做时,我想确保 South 正确迁移数据库(重命名数据库表和更改django_content_type中的引用或south_migrationhistory)。我知道如何将模型迁移到不同的应用程序,但是当我尝试重命名应用程序本身时,南方无法正确识别迁移历史。

I am renaming an application to a more suitable name. In doing so, I want to ensure that South properly migrates the database (renames database tables and changes references in django_content_type or south_migrationhistory). I know how to migrate a model to a different app, but when I try rename the app itself, South does not recognize the migration history properly.

不好的解决方案:将 old_app 重命名为 new_app 我可以离开 old_app / migrations 完整,并将新的迁移添加到此目录以将数据库迁移到引用 new_app

Undesirable solution: In renaming old_app to new_app I could leave old_app/migrations intact and add new migrations to this directory to migrate the database to reference new_app.

如果可能,我更愿意完全删除目录 old_app 。我还没有想到更好地解决这个问题。

If possible I would prefer to delete the directory old_app entirely. I have not yet thought of a better solution to this problem.

在没有丢失数据的情况下,使用Django South重命名应用程序的最佳方式是什么?

What is the best way to rename an app with Django South without losing data?

推荐答案

我同意Laksham你应该避免这种情况。但有时,我们必须。

I agree with Laksham that you should avoid this situation. But sometimes, we have to. I face this situation and proceed this way.

如果您想避免丢失数据,您可以将旧的应用程序数据转储到json文件中。

If you want to avoid losing data you can dump the old application data into a json file.

python manage.py dumpdata old_app --natural --indent=4 1> old_app.json

请注意 - 将自动导出内容类型的自然选项键(app_name,model)

Note the --natural option that will force the content types to be exported with their natural keys (app_name, model)

然后,您可以创建一个小命令来打开此json文件,并用new_app替换所有old_app引用。

Then you can create a small command to open this json file and to replace all the old_app references with the new_app.

这样的东西应该工作

class Command(BaseCommand):
    help = u"Rename app in json dump"

    def handle(self, *args, **options):
        try:
            old_app = args[0]
            new_app = args[1]
            filename = args[2]
        except IndexError:
            print u'usage :', __name__.split('.')[-1], 'old_app new_app dumpfile.json'
            return

        try:
            dump_file = open(filename, 'r')
        except IOError:
            print filename, u"doesn't exist"
            return

        objects = json.loads(dump_file.read())
        dump_file.close()

        for obj in objects:
            obj["model"] = obj["model"].replace(old_app, new_app, 1)

            if obj["fields"].has_key("content_type") and (old_app == obj["fields"]["content_type"][0]):
                obj["fields"]["content_type"][0] = new_app

        dump_file = open(filename, 'w')
        dump_file.write(json.dumps(objects, indent=4))
        dump_file.close()

然后重命名应用程序,更改INSTALLED_APPS中的名称。

Then rename the application, change the name in INSTALLED_APPS.

然后,您应该删除所有南迁,重新生成并应用新应用的初始迁移。然后运行SQL命令:

Then, you should remove all south migrations, regenerate and apply an initial migration for the new app. Then run the SQL command:

update django_content_type set app_label='new_app' where app_label='old_app'

然后为新应用程序启动南移,以创建表并加载json文件。

Then launch a south migrate for the new app in order to create the tables and load the json file.

python manage.py loaddata old_app.json

我在一个项目上做了类似的事情,似乎工作正常。

I've done something similar on a project and it seems to work ok.

我希望它有助于

这篇关于使用Django和South重命名应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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