BLOG

A LITTLE STORY ABOUT MY JOURNEY

Django dumpdata and loaddata

dumpdata command

  • It is a django management command, which can be use for database backup(import/export)
  • it can be model instance or all database

dumpdata:

basic command for backup all database

dumpdata  specific app

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

dumpdata specific table

  • Following command will dump only the content in django admin.logentrytable

 

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

     

dumpdata (–indent)

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)
  1. json
  2. xml
  3. yaml

loaddata command

  • This command can be use to load the fixtures(database dumps) into 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 excludingcontenttypes and auth.permissions tables

     
  • Now you can use loaddata command with a fresh database
4873 Total Views 1 Views Today