What is the best way to migrate data in django

前端 未结 6 1272
渐次进展
渐次进展 2021-01-30 11:27

After making some changes in my models (eg. new field in a model and a new model) what is the best way of reflecting these changes to my populated database?


PS: I

6条回答
  •  走了就别回头了
    2021-01-30 11:43

    Depends on the scope of the changes. If it's beyond an ALTER, you're doing major surgery. Make backups of model as well as database so you can go back.

    My preference is to put your new (revised, corrected, expanded) model in as a NEW application. It won't have URL's or anything, just a model.

    1. Creating the new model as a new application. Create tests, just to be sure it works.
    2. syncdb to build this interim implementation of the new model.
    3. Write a little one-time utility to query your old model, and load your new model. You might want to try this in pure SQL. I prefer to write a simple query, build and save loop.
    4. After the new model is loaded, you can dump this to a JSON file.

    Once you've pulled the data out of your old model, you can rebuild your DB in the preferred new format.

    1. Move the new model into your existing application.
    2. Drop the old versions of the application's tables.
    3. syncdb to build the new tables.
    4. Load the JSON file with the data.

提交回复
热议问题