2008-08-24 00:25:40 +02:00
|
|
|
=================================
|
|
|
|
Providing initial data for models
|
|
|
|
=================================
|
|
|
|
|
|
|
|
It's sometimes useful to pre-populate your database with hard-coded data when
|
2014-12-26 19:56:08 +01:00
|
|
|
you're first setting up an app. You can provide initial data via fixtures.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2012-02-19 10:02:52 +01:00
|
|
|
.. _initial-data-via-fixtures:
|
|
|
|
|
2008-08-24 00:25:40 +02:00
|
|
|
Providing initial data with fixtures
|
|
|
|
====================================
|
|
|
|
|
|
|
|
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
|
2010-08-19 21:27:44 +02:00
|
|
|
got some data is to use the :djadmin:`manage.py dumpdata <dumpdata>` command.
|
2014-09-21 16:00:19 +02:00
|
|
|
Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML
|
|
|
|
(with PyYAML_ installed) documents. The :doc:`serialization documentation
|
|
|
|
</topics/serialization>` has more details about each of these supported
|
|
|
|
:ref:`serialization formats <serialization-formats>`.
|
|
|
|
|
|
|
|
.. _PyYAML: http://www.pyyaml.org/
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
As an example, though, here's what a fixture for a simple ``Person`` model might
|
|
|
|
look like in JSON:
|
|
|
|
|
|
|
|
.. code-block:: js
|
|
|
|
|
|
|
|
[
|
|
|
|
{
|
2008-11-06 11:24:24 +01:00
|
|
|
"model": "myapp.person",
|
2008-08-24 00:25:40 +02:00
|
|
|
"pk": 1,
|
|
|
|
"fields": {
|
|
|
|
"first_name": "John",
|
2008-11-06 11:24:24 +01:00
|
|
|
"last_name": "Lennon"
|
2008-08-24 00:25:40 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2008-11-06 11:24:24 +01:00
|
|
|
"model": "myapp.person",
|
2008-08-24 00:25:40 +02:00
|
|
|
"pk": 2,
|
|
|
|
"fields": {
|
|
|
|
"first_name": "Paul",
|
2008-11-06 11:24:24 +01:00
|
|
|
"last_name": "McCartney"
|
2008-08-24 00:25:40 +02:00
|
|
|
}
|
Fixed a whole bunch of small docs typos, errors, and ommissions.
Fixes #8358, #8396, #8724, #9043, #9128, #9247, #9267, #9267, #9375, #9409, #9414, #9416, #9446, #9454, #9464, #9503, #9518, #9533, #9657, #9658, #9683, #9733, #9771, #9835, #9836, #9837, #9897, #9906, #9912, #9945, #9986, #9992, #10055, #10084, #10091, #10145, #10245, #10257, #10309, #10358, #10359, #10424, #10426, #10508, #10531, #10551, #10635, #10637, #10656, #10658, #10690, #10699, #19528.
Thanks to all the respective authors of those tickets.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10371 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2009-04-03 20:30:54 +02:00
|
|
|
}
|
2008-08-24 00:25:40 +02:00
|
|
|
]
|
2008-11-06 11:24:24 +01:00
|
|
|
|
2008-08-24 00:25:40 +02:00
|
|
|
And here's that same fixture as YAML:
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
- model: myapp.person
|
|
|
|
pk: 1
|
|
|
|
fields:
|
|
|
|
first_name: John
|
|
|
|
last_name: Lennon
|
|
|
|
- model: myapp.person
|
2008-08-28 14:49:11 +02:00
|
|
|
pk: 2
|
2008-08-24 00:25:40 +02:00
|
|
|
fields:
|
|
|
|
first_name: Paul
|
|
|
|
last_name: McCartney
|
2008-11-06 11:24:24 +01:00
|
|
|
|
2008-12-07 04:41:23 +01:00
|
|
|
You'll store this data in a ``fixtures`` directory inside your app.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2012-07-15 11:25:13 +02:00
|
|
|
Loading data is easy: just call :djadmin:`manage.py loaddata <loaddata>`
|
|
|
|
``<fixturename>``, where ``<fixturename>`` is the name of the fixture file
|
|
|
|
you've created. Each time you run :djadmin:`loaddata`, 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 :djadmin:`loaddata`
|
|
|
|
again, you'll wipe out any changes you've made.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2012-02-19 10:02:52 +01:00
|
|
|
Where Django finds fixture files
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
By default, Django looks in the ``fixtures`` directory inside each app for
|
|
|
|
fixtures. You can set the :setting:`FIXTURE_DIRS` setting to a list of
|
|
|
|
additional directories where Django should look.
|
|
|
|
|
|
|
|
When running :djadmin:`manage.py loaddata <loaddata>`, you can also
|
2013-08-20 19:03:33 +02:00
|
|
|
specify a path to a fixture file, which overrides searching the usual
|
|
|
|
directories.
|
2012-02-19 10:02:52 +01:00
|
|
|
|
2008-08-24 00:25:40 +02:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Fixtures are also used by the :ref:`testing framework
|
|
|
|
<topics-testing-fixtures>` to help set up a consistent test environment.
|