Django dumpdata

Summary: in this tutorial, you’ll learn how to use the Django dumpdata command to export the database into files.

Introduction to the Django dumpdata command

Sometimes, you want to move some common data from a test database to the production database.

To do that you use the dumpdata command to export data in the test database into a file and import it to the production database using the loaddata command.

The dumpdata command has many options that allow you to:

  • Export all model instances of all the apps (the whole database) into a file.
  • Export all model instances of an app into a file.
  • Export some model instances (some tables in the database) into a file.

The format of the output file can be xml, json, jsonl, or yaml. The following dump the whole database into a data.json file:

python manage.py dumpdata > data.jsonCode language: Python (python)

If you open the data.json file, you’ll see lots of data. For example, the following shows the Employee’s instance:

...
{
    "model": "hr.employee",
    "pk": 5,
    "fields": {
      "first_name": "John",
      "last_name": "Doe",
      "contact": null,
      "department": 1,
      "compensations": [1, 2]
    }
  },
...Code language: Python (python)

The sample model contains the following information:

  • The model name (hr.employee).
  • The primary key value (pk).
  • The fields (first_name, last_name, contact, department, and compensations) of the Employee model.

If you want to export the whole database to another format like XML, you need to specify the format option:

python manage.py dumpdata > filename --format file_formatCode language: Python (python)

The file_format can be json, jsonl, xml, and yaml.

For example, the following command exports the whole database into an XML file:

python manage.py dumpdata > data.xml --format xmlCode language: Python (python)

Exporting data from a specific app

To export data of a specific app, you specify the app name:

python manage.py dumpdata app_name > filename.jsonCode language: Python (python)

For example, the following command exports the model instances of the hr app:

python manage.py dumpdata hr > hr.jsonCode language: Python (python)

Exporting data from a specific model

To dump the data of a specific table, you specify the app name and model name as follows:

python manage.py app_name.model_name > filenameCode language: Python (python)

For example, the following command dumps all the instances of the Employee table in the HR application:

python manage.py dumpdata hr.employee > hr_employee.jsonCode language: Python (python)

Exporting data by excluding one or more models

Sometimes, you want to export data from all models except for one or more models. In this case, you can use the --exclude option:

python manage.py --exclude app_name.model_name > filenameCode language: Python (python)

Note that the command may contain multiple --exclude options so that you can exclude multiple models.

For example, the following export data from the whole database except for the contact model:

python manage.py --exclude hr.contact > data.jsonCode language: Python (python)

The following command exports data from the whole database except for the contact and department models:

python manage.py --exclude hr.contact --exclude department > data.jsonCode language: Python (python)

Summary

  • Use the Django dumpdata command to export data of one or more models.
Did you find this tutorial helpful ?