Providing initial data for models
If you want to automatically load initial data for an app, create a . Migrations are run when setting up the test database, so the data will be available there, subject to some limitations.
Providing data with fixtures
You can also provide data using fixtures, however, this data isn’t loaded automatically, except if you use .
A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you’ve already got some data is to use the manage.py dumpdata
command. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML (with installed) documents. The serialization documentation has more details about each of these supported .
And here’s that same fixture as YAML:
pk: 1
fields:
first_name: John
- model: myapp.person
pk: 2
fields:
last_name: McCartney
You’ll store this data in a fixtures
directory inside your app.
You can load data by calling <fixturename>
, where <fixturename>
is the name of the fixture file you’ve created. Each time you run , the data will be read from the fixture and re-loaded into the database. Note this means that if you change one of the rows created by a fixture and then run loaddata
again, you’ll wipe out any changes you’ve made.
When running , you can also specify a path to a fixture file, which overrides searching the usual directories.
See also
Fixtures are also used by the testing framework to help set up a consistent test environment.