Moving Data from One Database to Another with Django

So I have a django database I'm working on and I decided I wanted to do the development in sqlite3 instead of mysql. I decided to do this because it makes it easier, for example, to have someone else work on HTML/CSS if I can just give them a directory, tell them to run a bash script and go to http://localhost:8000, rather than them having to do all that AND setup a mysql server. Sure, that can also be done with a script, but with sqlite things are just a hell of a lot easier in some ways.

I decided I wanted to take the data out of my mysql database and load it into a sqlite db. Not so simple, you can't just take an SQL dump from mysql and import it into sqlite3. I started Googling around for "mysql to sqlite" and didn't really get anywhere. I then realized that Django can already talk to sqlite and mysql transparently without me having to know any SQL. So I thought about writing my own python modules to do this, but it turns out someone has already done it.

The django-extensions project has a command called "dumpscript" that "Generates a Python script that will repopulate the database using objects. The advantage of this approach is that it is easy to understand, and more flexible than directly populating the database, or using XML." This was exactly what I wanted.



I love django-extensions. Very helpful library. But wouldn't the stock ' dumpdata' have done the trick?

Dumpdata/loaddata is not really that easy with full imports.

Many times, models depend on the proper order of importing so a big import fails. 'loaddata' uses the ORM, so it will trigger your models, which are now operating in a very unusual half-empty setup. loaddata seems to create models when the foreignkeys don't exist yet, breaking the import of the actual data later on. The data also includes the contenttypes table, so it should be cleaned first.

I wish it would be so easy, but sadly in practice I didn't always experience that. In the end I typically dumped the data of a few apps separately, and imported them one by one. :-/

You might find django-smuggler useful as well.

Add new comment