Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to migrate data in django

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 wanted to see many solutions in one place rated. Apparently more solutions are already listed here.

like image 322
hamdiakoguz Avatar asked Oct 06 '08 20:10

hamdiakoguz


People also ask

How does Django run data migration?

Django has a great documentation on migrations workflow and what they actually do under the hood. Most of the work is automatically handled by just two commands: python manage.py makemigrations: creates a file for your changes in the model. python manage.py migrate: scan that file to create database schema.

What is Django framework how it helps in code data migration?

Django Web Framework offers a shortcut to full integration with your application's database. It provides CRUD (create, read, update, delete) functionality, HttpResponse and cross-site scripting, supplies user management capabilities, offers software administration features and more.

What is a Django migration?

Migration is a way of applying changes that we have made to a model, into the database schema. Django creates a migration file inside the migration folder for each model to create the table schema, and each table is mapped to the model of which migration is created.


2 Answers

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.

like image 121
shacker Avatar answered Sep 18 '22 04:09

shacker


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)
like image 45
Farinha Avatar answered Sep 20 '22 04:09

Farinha