What is the best way to migrate data in django

前端 未结 6 1269
渐次进展
渐次进展 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.
    0 讨论(0)
  • 2021-01-30 11:44

    Perform these steps in order may help you:

    For more details,

    clickhere: http://south.readthedocs.org/en/latest/

    1) python manage.py schemamigration apps.appname --initial

    Above step creates migration folder as default.

    2) python manage.py migrate apps.appname --fake

    generates a fake migration.

    3) python manage.py schemamigration apps.appname --auto

    Then you can add fields as you wish and perform the above command.

    4) python manage.py migrate apps.appname

    Then migrate the files to the database.

    0 讨论(0)
  • 2021-01-30 11:45

    Look with manage.py sqlall what the parameters are for the new columns and manually add them in your database with Alter table statements. This way you don't have to redo your database; It requires some SQL knowledge though...

    Take a look here (Scroll down to "Making Changes to a Database Schema")

    0 讨论(0)
  • 2021-01-30 11:50

    I've asked a similar question here and got quite a few answers.

    There are quite a lot of ways of doing it, like manually doing the dumping and reloading with SQL, using fixtures or using one of the "emerging" schema-evolution packages for Django:

    • Django Evolution
    • South
    • dmigrations (there's a DjangoCon video of a panel on schema-evolution in Django where these 3 solutions are discussed)
    0 讨论(0)
  • 2021-01-30 11:56

    Another technique is to use the dumpdata and loaddata arguments to manage.py, killing your database in-between:

    1. python manage.py dumpdata > dump.json
    2. With an external tool, drop any affected tables, or kill the whole db
    3. python manage.py loaddata dump.json

    See manage.py docs for more.

    0 讨论(0)
  • 2021-01-30 12:00

    Django now has its own built-in migrations, documented at:

    https://docs.djangoproject.com/en/dev/topics/migrations/

    0 讨论(0)
提交回复
热议问题