FrankWiles.com

TIL: Today I Learned

Django dumpdata only YOUR data

Ok so I lied. I didn’t “learn” this today, but I had to rediscover it for myself again today so it counts right?

A common situation you may face with Django is you have some small amount of data, let’s say less than 25MBs, in a database somewhere. Usually locally, but it doesn’t really matter and you want to easily push this data into another location.

Most often it’s that you’ve deployed your app to a dev or prod environment and it’s ran its own migrations. So you can’t super easily use pg_dump and pg_restore as all of the tables and things already exist.

Django has a handy dumpdata command but if you just run ./manage.py dumpdata --format=json > data.json and then try to load it you’ll get exceptions related to some data that already exists!

I know what you’re thinking, I just created this DB and ran migrations how is there data already? It’s data Django creates for you such as permissions and content types.

For 99% of Django sites this is what you want:

shell
 python manage.py dumpdata \
      --natural-foreign \
      --natural-primary \
      --indent 2 \
      --exclude contenttypes \
      --exclude auth.Permission \
      --exclude admin.logentry \
      --exclude sessions \
      > data.json

This excludes the data from the Permission model, sessions and contenttypes entirely. I’ve also added the Django admin logentry model as I don’t care to bring this data over in most situations but you might use it as a real changelog so consider retaining that.

This gives you an easy to import JSON file using ./manage.py loaddata data.json that shouldn’t have any conflicts with any data Django generates for you automatically.

WARNING: Remember how I said “a small amount of data” above? I do mean that. This method is NOT appropriate for backing and and restoring large databases at all. You should investigate what the proper tools are for your database backend and use those instead.

This is just a convienient way of shuttling around a small amount of data without having to ensure your target database is entirely empty.

RSS Feed All Content | RSS Feed All TIL