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.json file

python manage.py dumpdata > db.json

dumpdata for backup specific app

Following command will dump the content in django admin app into admin.json file

python manage.py dumpdata admin > admin.json

dumpdata for backup specific table

Following command will dump only the content in django admin.logentry table

python manage.py dumpdata admin.logentry > logentry.json

Following command will dump the content in django auth.user table

python manage.py dumpdata auth.user > user.json

dumpdata (–exclude)

You can use --exclude option to specify apps/tables which don’t need being dumped

Following command will dump the whole database with out including auth.permission table content

python manage.py dumpdata --exclude auth.permission > db.json

dumpdata (–indent)

By default, dumpdata will output all data on a single line. It isn’t easy for humans to read

You can use the --indent option to pretty-print the output with a number of indentation spaces

python manage.py dumpdata auth.user --indent 2 > user.json

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
python manage.py dumpdata auth.user --indent 2 --format xml > user.xml

loaddata command

This command can be use to load the fixtures(database dumps) into database

python manage.py loaddata user.json

This command will add the user.json file content into the database

Restore fresh database

When you backup whole database by using dumpdata command, it will backup all the database tables

If you use this database dump to load the fresh database(in another django project), it can be causes IntegrityError (If you loaddata in same database it works fine)

To fix this problem, make sure to backup the database by excluding contenttypes and auth.permissions tables

python manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json

Now you can use loaddata command with a fresh database

python manage.py loaddata db.json

Explore More Django Posts

Efficient Django Project Settings with Split Settings Library

Learn how to efficiently manage your Django project settings with the Split Settings library. Use environment variables, keep sensitive information i…

Read More
Integrating Flake8 with Django: Best Practices

Learn how to integrate Flake8 with Django projects and enforce code quality. Follow our step-by-step guide and optimize your Django workflow with Fla…

Read More
Django Authentication and Authorization with JWT

Learn how to implement JSON Web Token (JWT) based authentication and authorization in Django web applications with step-by-step guide and code exampl…

Read More
Best Practices for Django Development: Tips and Tricks

Learn the best practices for Django development, including project structure, code organization, testing, and deployment. Build high-quality web apps.

Read More
Django Middleware: Tips, Tricks and Examples

Learn how to use Django Middleware to improve your app's performance and security. Includes examples and best practices.

Read More
Django Production Deployment: Best Practices & Checklist

Learn the best practices and checklist for deploying a Django application to production. Includes tips on web servers, databases, caching, security, …

Read More