2021-07-13 15:25:37 +08:00
|
|
|
======================================
|
|
|
|
How to provide initial data for models
|
|
|
|
======================================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2022-03-11 16:11:42 +08:00
|
|
|
It's sometimes useful to prepopulate 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.
|
|
|
|
|
2022-09-24 22:27:25 +08:00
|
|
|
Provide initial data with migrations
|
|
|
|
====================================
|
2018-03-08 23:58:19 +08:00
|
|
|
|
2022-09-24 22:27:25 +08:00
|
|
|
To automatically load initial data for an app, create a
|
2018-03-08 23:58:19 +08:00
|
|
|
: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:
|
|
|
|
|
2022-09-24 22:27:25 +08:00
|
|
|
Provide data with fixtures
|
|
|
|
==========================
|
2018-03-08 23:58:19 +08:00
|
|
|
|
2022-09-25 21:08:24 +08:00
|
|
|
You can also provide data using :ref:`fixtures <fixtures-explanation>`,
|
|
|
|
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
|
2022-03-11 16:11:42 +08:00
|
|
|
from the fixture and reloaded into the database. Note this means that if you
|
2012-07-15 17:25:13 +08:00
|
|
|
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
|
|
|
|
2022-09-24 22:27:25 +08:00
|
|
|
Tell Django where to look for fixture files
|
|
|
|
-------------------------------------------
|
|
|
|
|
|
|
|
By default, Django looks for fixtures in the ``fixtures`` directory inside each
|
|
|
|
app for, so the command ``loaddata sample`` will find the file
|
|
|
|
``my_app/fixtures/sample.json``. This works with relative paths as well, so
|
|
|
|
``loaddata my_app/sample`` will find the file
|
|
|
|
``my_app/fixtures/my_app/sample.json``.
|
|
|
|
|
|
|
|
Django also looks for fixtures in the list of directories provided in the
|
|
|
|
:setting:`FIXTURE_DIRS` setting.
|
|
|
|
|
|
|
|
To completely prevent default search form happening, use an absolute path to
|
|
|
|
specify the location of your fixture file, e.g. ``loaddata /path/to/sample``.
|
2012-02-19 17:02:52 +08:00
|
|
|
|
2022-09-24 22:27:25 +08:00
|
|
|
.. admonition:: Namespace your fixture files
|
2012-02-19 17:02:52 +08:00
|
|
|
|
2022-09-24 22:27:25 +08:00
|
|
|
Django will use the first fixture file it finds whose name matches, so if
|
|
|
|
you have fixture files with the same name in different applications, you
|
|
|
|
will be unable to distinguish between them in your ``loaddata`` commands.
|
|
|
|
The easiest way to avoid this problem is by *namespacing* your fixture
|
|
|
|
files. That is, by putting them inside a directory named for their
|
|
|
|
application, as in the relative path example above.
|
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.
|