Django – selective restore of DB dump

If you are running a Django application, hopefully you have regular backups of the DB.  We had a customer who realized they deleted something over a month ago and needed the deleted objects restored.  Instead of just reverting the entire DB and losing all of the new information, I wrote a simple script that will create a fixture of everything that was deleted that can then be used with loaddata.

Thanks goes to Constantin Berzan for how to programmatically enumerate objects that will get cascade-deleteded in Django.

Step By Step

  1. In a local development environment restore the DB to the version with the objects you need to restore into production.
  2. Run  python shell
  3. In the shell type  execfile('') where ‘’ is what you named your customized version of the script below.
  4. On the production environment, place the dump.json file generated in step 3 in the same directory as and run python loaddata dump.json

Save this file in the same directory as your Django on your local development environment.  Update what you need to dump as needed



Travis Payton
Follow Me

  1 comment for “Django – selective restore of DB dump

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: