Using Dumpdata and Loaddata

Using Dumpdata and Loaddata
Dumpdata
Dumpdata is a command available among Django’s default commands, accessible via python manage.py. Its purpose is to create a backup (fixture) of your database in a specified format. If you wish, you can use the loaddata command to write this backup into a newly created database. Those curious about the source code of the dumpdata command can click the link I provided to study it and get more detailed information firsthand: dumpdata.py
Usage
General usage: python manage.py dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]]
Parameters
--all -aWhen these parameters are used, Django uses the base manager to dump all models.--format FORMATWith the format parameter, you can specify which format to use when dumping. The default format is json and there are three formats available: xml, json, yaml.--indent INDENTSpecifies the indentation level of the output. Default is JSON. Supported formats are listed in the serialization formats documentation.--exclude EXCLUDE, -e EXCLUDEDuring backup, there may be certain parts you don’t want to back up — this parameter is for that purpose.--database DATABASEA helpful parameter for those using multiple databases — it specifies which database to use for the backup. Defaults to the default database.--natural-foreignUses thenatural_key()model method to serialize any foreign key and many-to-many relationships with objects that define this method. If you are dumpingcontrib.authPermission objects orcontrib.contenttypesContentType objects, you should probably use this flag. For more details about this and the next option, see the documentation on natural keys.--natural-primaryOmits the primary key in the serialized data for this object, as it can be calculated during deserialization.--pks PRIMARY_KEYSOutputs only the objects specified by a comma-separated list of primary keys. This is only available when dumping one model. By default, all records for the model are output.--output OUTPUT, -o OUTPUTSpecifies a file to write the serialized data to. By default, the data goes to standard output. Can also be used like> output.json.
Example Usage
python manage.py dumpdata output.json --format json --database defaultWithout specifying a model name — dumps the entire selected database.python manage.py dumpdata auth output.json --format json --database defaultDumps only the auth model.python manage.py dumpdata auth.user output.json --format json --database defaultDumps only the user section within the auth model.python manage.py dumpdata --exclude admin output.json --format json --database defaultDumps all data except admin.python manage.py dumpdata mymodel output.json --format json --database myseconddbDumps the data from the model namedmymodelin the database namedmyseconddb(my second database).
Warning
If you get the RelatedObjectDoesNotExist error during the dumpdata command, check out this address: https://code.djangoproject.com/ticket/28972 and use it as python manage.py dumpdata -o output.json --format json — the problem will be resolved.
Loaddata
The loaddata command lets us load the data created with dumpdata into a newly created database. The source code address for loaddata is: loaddata.py
Usage
General usage: python manage.py loaddata fixture [fixture ...] where fixture is the file created with dumpdata.
Parameters
--database DATABASESame as above — you can select a database. Defaults to thedefaultdatabase.--ignorenonexistent, -iAllows ignoring fields and models that don’t exist.--app APP_LABELAllows loading a specific app.--format FORMATEnter the format of the file you created with dumpdata.--exclude EXCLUDE, -e EXCLUDESame purpose — used to exclude applications or models during loading.
Example Usage
python manage.py loaddata mydata.json --format jsonpython manage.py loaddata mydata.json --database mydbpython manage.py loaddata mydata.json --format xml --database defaultpython manage.py loaddata mydata.json --exclude auth --database default
Hakan Çelik