Django dumpdata and loaddata
dumpdata command
- It is a django management command, which can be use to backup(export) you model instances or whole database
dumpdata for basic database dump
- Following command will dump whole database in to a
db.jsonfile
./manage.py dumpdata > db.json
dumpdata for backup specific app
- Following command will dump the content in django
adminapp intoadmin.jsonfile
./manage.py dumpdata admin > admin.json
dumpdata for backup specific table
- Following command will dump only the content in django
admin.logentrytable
./manage.py dumpdata admin.logentry > logentry.json
- Following command will dump the content in django
auth.usertable
./manage.py dumpdata auth.user > user.json
dumpdata (--exclude)
You can use
--excludeoption to specify apps/tables which don't need being dumpedFollowing command will dump the whole database with out including
auth.permissiontable content
./manage.py dumpdata --exclude auth.permission > db.json
dumpdata (--indent)
By default,
dumpdatawill output all data on a single line. It isn’t easy for humans to readYou can use the
--indentoption to pretty-print the output with a number of indentation spaces
./manage.py dumpdata auth.user --indent 2 > user.json
- Example output of above command is below

dumpdata (--format)
By default, dumpdata will format its output in JSON
You can specify the format using --format option
Command supports for following formats(serialization formats)
- json
- xml
- yaml
./manage.py dumpdata auth.user --indent 2 --format xml > user.xml
- Above command output an xml file(user.xml)

loaddata command
- This command can be use to load the fixtures(database dumps) into database
./manage.py loaddata user.json
- This command will add the
user.jsonfile content into the database
Restore fresh database
When you backup whole database by using
dumpdatacommand, it will backup all the database tablesIf you use this database dump to load the fresh database(in another django project), it can be causes
IntegrityError(If youloaddatain same database it works fine)To fix this problem, make sure to backup the database by excluding
contenttypesandauth.permissionstables
./manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
- Now you can use
loaddatacommand with a fresh database
./manage.py loaddata db.jsonWritten by eranga bandara
Related protips
7 Responses
Thanks for this tutorial. The steps worked for me, but I found that when I tried to restore, I got an out of memory error, because my file was too large. I ended up using raw postgres commands:
export: pg_dump <dbname> -t <tablename> -f out.sql
import: psql <dbname> -f out.sql
There is lots of other cases where loaddata and dumpdata commands don't work. So pg_dump and psql are good, but the downside of them is that you lose the database configuration that is stored in the project settings (or the environment if you are 12factor aware).
Thank you very much! Paragraph "Restore fresh database" saved me!
Hi, thanks for this post. I was able to use it to save some dats into json but when I tried to load same data into the db, I got the error:
raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), pk)
local variable 'pk' referenced before assignment
How do I go around this error? Thanks
Thanks! This is awesome. For some reason I actually got my Webfaction Django to accept fixtures. Def bookmark on this exact page!
Good article. Here is a blog explaining loading fixtures in django with a video tutorial:- http://newprograminglogics.blogspot.com/2018/09/load-initial-data-into-django-model.html
Thanks so much, this save me from slot of trouble.