How to move a model between two Django apps (Django 1.7)

后端 未结 11 1658
日久生厌
日久生厌 2020-11-28 17:35

So about a year ago I started a project and like all new developers I didn\'t really focus too much on the structure, however now I am further along with Django it has start

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 18:20

    1. change the names of old models to ‘model_name_old’
    2. makemigrations
    3. make new models named ‘model_name_new’ with identical relationships on the related models (eg. user model now has user.blog_old and user.blog_new)
    4. makemigrations
    5. write a custom migration that migrates all the data to the new model tables
    6. test the hell out of these migrations by comparing backups with new db copies before and after running the migrations
    7. when all is satisfactory, delete the old models
    8. makemigrations
    9. change the new models to the correct name ‘model_name_new’ -> ‘model_name’
    10. test the whole slew of migrations on a staging server
    11. take your production site down for a few minutes in order to run all migrations without users interfering

    Do this individually for each model that needs to be moved. I wouldn’t suggest doing what the other answer says by changing to integers and back to foreign keys There is a chance that new foreign keys will be different and rows may have different IDs after the migrations and I didn’t want to run any risk of mismatching ids when switching back to foreign keys.

提交回复
热议问题