How to rename a Django app and migrate data from an app to the other

前端 未结 3 1434
生来不讨喜
生来不讨喜 2020-12-16 08:23

I have a Django app named app1 with models and migrations files. I renamed this app to app2 and I fixed all imports, urls etc... I now have a probl

3条回答
  •  有刺的猬
    2020-12-16 08:42

    Renaming an app is always a tricky issue.

    If you do the migration like a simple table renaming migration, at any moment the apps.get_model() for the old app cannot work because the app simply doesn't exist.

    I found this answer. I know you are not using south, but I think it might work the same way, just skip the south steps.

    Basically, you have to:

    1. Dump the data, before rename, into a json file

    2. Run the script in the answer to rename references in the json file from the app1 to app2

    3. Rename app1 to app2 (all import references, settings.py, etc)

    4. Run the migrations to create the tables for app2

    5. Load the data from json file to the database

    6. Drop the app1 tables

    I hope this help.

提交回复
热议问题