How to Dump and Loaddata in Django

How to Dump and Loaddata in Django

 

How to Dump and Loaddata in Django

 

Data management in web applications requires the ability to dump and load data in Django. Django offers a straightforward method to load data from a file into a database and to dump data from a database to a file.

 

Dumpdata

 

Basic Dumpdata Command

 

This command used for dump whole django project data

 

python manage.py dumpdata > dumpdata.json

 

Sepecific App Dumpdata Command

 

This command used for dump specific django app data

 

python manage.py dumpdata app_name > dumpdata.json

 

Sepecific App and Specific Model (Table) Dumpdata Command

 

This command used for dump specific django app data

 

python manage.py dumpdata app_name.model_name > dumpdata.json

 

Exclude or Eliminate Sepecific Model (Table) data Command

 

This command used for dump specific database data

 

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

 

Sepecific Database Dumpdata Command

 

This command used for dump specific database data

 

python manage.py dumpdata --database=db_name > dumpdata.json

 

Sepecific Database with Specific Model (Table) Dumpdata Command

 

This command used for dump specific database data

 

python manage.py dumpdata --database=db_name app_name.model_name > dumpdata.json

 

Loaddata

 

Common Loaddata Command

 

python manage.py loaddata dumpdata.json

 

Integrity Error

 

Common problem is when dump after loaddata you have to face some integrity error.The reason behind Permission Table and ContentTypes already contain some data so it triggered some problem

 

How to fix it?

 

python manage.py dumpdata -e auth.permission -e contenttypes > dumpdata.json

 

Now you got perfect data

 

python manage.py loaddata dumpdata.json

 

 

Thank you happy coding!!!

Share