Fixtures are initial data for the database.
The most straightforward way when you have some existing data already is to use the command dumpdata
./manage.py dumpdata > databasedump.json # full database
./manage.py dumpdata myapp > databasedump.json # only 1 app
./manage.py dumpdata myapp.mymodel > databasedump.json # only 1 model (table)
This will create a json file which can be imported again by using
./manage.py loaddata databasedump.json
When using the loadddata
without specifying a file, Django will look for a fixtures
folder in your app or the list of directories provided in the FIXTURE_DIRS
in settings, and use its content instead.
/myapp
/fixtures
myfixtures.json
morefixtures.xml
Possible file formats are: JSON, XML or YAML
Fixtures JSON example:
[
{
"model": "myapp.person",
"pk": 1,
"fields": {
"first_name": "John",
"last_name": "Lennon"
}
},
{
"model": "myapp.person",
"pk": 2,
"fields": {
"first_name": "Paul",
"last_name": "McCartney"
}
}
]
Fixtures YAML example:
- model: myapp.person
pk: 1
fields:
first_name: John
last_name: Lennon
- model: myapp.person
pk: 2
fields:
first_name: Paul
last_name: McCartney
Fixtures XML example:
<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0">
<object pk="1" model="myapp.person">
<field type="CharField" name="first_name">John</field>
<field type="CharField" name="last_name">Lennon</field>
</object>
<object pk="2" model="myapp.person">
<field type="CharField" name="first_name">Paul</field>
<field type="CharField" name="last_name">McCartney</field>
</object>
</django-objects>