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

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

问题描述

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

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've faced this situation in the past and I've managed it 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

注意 --natural 选项,该选项将强制使用其自然键(app_name、模型)导出内容类型

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

然后您可以创建一个小命令来打开这个 json 文件并将所有 old_app 引用替换为 new_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.

这样的东西应该可以工作

Something like this should work

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天全站免登陆