2008-08-24 06:25:40 +08:00
|
|
|
=================================
|
|
|
|
Providing initial data for models
|
|
|
|
=================================
|
|
|
|
|
|
|
|
It's sometimes useful to pre-populate your database with hard-coded data when
|
2018-03-08 23:58:19 +08:00
|
|
|
you're first setting up an app. You can provide initial data with migrations or
|
|
|
|
fixtures.
|
|
|
|
|
|
|
|
Providing initial data with migrations
|
|
|
|
======================================
|
|
|
|
|
|
|
|
If you want to automatically load initial data for an app, create a
|
|
|
|
:ref:`data migration <data-migrations>`. Migrations are run when setting up the
|
|
|
|
test database, so the data will be available there, subject to :ref:`some
|
|
|
|
limitations <test-case-serialized-rollback>`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-02-19 17:02:52 +08:00
|
|
|
.. _initial-data-via-fixtures:
|
|
|
|
|
2018-03-08 23:58:19 +08:00
|
|
|
Providing data with fixtures
|
|
|
|
============================
|
|
|
|
|
|
|
|
You can also provide data using fixtures, however, this data isn't loaded
|
|
|
|
automatically, except if you use :attr:`.TransactionTestCase.fixtures`.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
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-20 03:27:44 +08:00
|
|
|
got some data is to use the :djadmin:`manage.py dumpdata <dumpdata>` command.
|
2014-09-21 22:00:19 +08: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>`.
|
|
|
|
|
2018-12-27 17:48:37 +08:00
|
|
|
.. _PyYAML: https://pyyaml.org/
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
As an example, though, here's what a fixture for a ``Person`` model might look
|
|
|
|
like in JSON:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
.. code-block:: js
|
|
|
|
|
|
|
|
[
|
|
|
|
{
|
2008-11-06 18:24:24 +08:00
|
|
|
"model": "myapp.person",
|
2008-08-24 06:25:40 +08:00
|
|
|
"pk": 1,
|
|
|
|
"fields": {
|
|
|
|
"first_name": "John",
|
2008-11-06 18:24:24 +08:00
|
|
|
"last_name": "Lennon"
|
2008-08-24 06:25:40 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2008-11-06 18:24:24 +08:00
|
|
|
"model": "myapp.person",
|
2008-08-24 06:25:40 +08:00
|
|
|
"pk": 2,
|
|
|
|
"fields": {
|
|
|
|
"first_name": "Paul",
|
2008-11-06 18:24:24 +08:00
|
|
|
"last_name": "McCartney"
|
2008-08-24 06:25:40 +08: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-04 02:30:54 +08:00
|
|
|
}
|
2008-08-24 06:25:40 +08:00
|
|
|
]
|
2008-11-06 18:24:24 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
And here's that same fixture as YAML:
|
|
|
|
|
2018-09-11 01:00:34 +08:00
|
|
|
.. code-block:: yaml
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
- model: myapp.person
|
|
|
|
pk: 1
|
|
|
|
fields:
|
|
|
|
first_name: John
|
|
|
|
last_name: Lennon
|
|
|
|
- model: myapp.person
|
2008-08-28 20:49:11 +08:00
|
|
|
pk: 2
|
2008-08-24 06:25:40 +08:00
|
|
|
fields:
|
|
|
|
first_name: Paul
|
|
|
|
last_name: McCartney
|
2008-11-06 18:24:24 +08:00
|
|
|
|
2008-12-07 11:41:23 +08:00
|
|
|
You'll store this data in a ``fixtures`` directory inside your app.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2019-06-17 22:54:55 +08:00
|
|
|
You can load data by calling :djadmin:`manage.py loaddata <loaddata>`
|
2012-07-15 17:25:13 +08:00
|
|
|
``<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 06:25:40 +08:00
|
|
|
|
2012-02-19 17:02:52 +08: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-21 01:03:33 +08:00
|
|
|
specify a path to a fixture file, which overrides searching the usual
|
|
|
|
directories.
|
2012-02-19 17:02:52 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Fixtures are also used by the :ref:`testing framework
|
|
|
|
<topics-testing-fixtures>` to help set up a consistent test environment.
|