Django Database Setup Fixtures


Fixtures are initial data for the database. The most straightforward way when you have some existing data already is to use the command dumpdata

 ./ dumpdata > databasedump.json                # full database
 ./ dumpdata myapp > databasedump.json          # only 1 app
 ./ dumpdata myapp.mymodel > databasedump.json  # only 1 model (table)

This will create a json file which can be imported again by using

./ 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.


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
    first_name: John
    last_name: Lennon
- model: myapp.person
  pk: 2
    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 pk="2" model="myapp.person">
        <field type="CharField" name="first_name">Paul</field>
        <field type="CharField" name="last_name">McCartney</field>