2021-07-13 07:25:37 +00:00
|
|
|
======================================
|
|
|
|
How to provide initial data for models
|
|
|
|
======================================
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2022-03-11 08:11:42 +00:00
|
|
|
It's sometimes useful to prepopulate your database with hard-coded data when
|
2018-03-08 15:58:19 +00: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-23 22:25:40 +00:00
|
|
|
|
2012-02-19 09:02:52 +00:00
|
|
|
.. _initial-data-via-fixtures:
|
|
|
|
|
2018-03-08 15:58:19 +00: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-23 22:25:40 +00: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-19 19:27:44 +00:00
|
|
|
got some data is to use the :djadmin:`manage.py dumpdata <dumpdata>` command.
|
2014-09-21 14:00:19 +00: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 09:48:37 +00:00
|
|
|
.. _PyYAML: https://pyyaml.org/
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
As an example, though, here's what a fixture for a ``Person`` model might look
|
|
|
|
like in JSON:
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
.. code-block:: js
|
|
|
|
|
|
|
|
[
|
|
|
|
{
|
2008-11-06 10:24:24 +00:00
|
|
|
"model": "myapp.person",
|
2008-08-23 22:25:40 +00:00
|
|
|
"pk": 1,
|
|
|
|
"fields": {
|
|
|
|
"first_name": "John",
|
2008-11-06 10:24:24 +00:00
|
|
|
"last_name": "Lennon"
|
2008-08-23 22:25:40 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2008-11-06 10:24:24 +00:00
|
|
|
"model": "myapp.person",
|
2008-08-23 22:25:40 +00:00
|
|
|
"pk": 2,
|
|
|
|
"fields": {
|
|
|
|
"first_name": "Paul",
|
2008-11-06 10:24:24 +00:00
|
|
|
"last_name": "McCartney"
|
2008-08-23 22:25:40 +00: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 18:30:54 +00:00
|
|
|
}
|
2008-08-23 22:25:40 +00:00
|
|
|
]
|
2008-11-06 10:24:24 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
And here's that same fixture as YAML:
|
|
|
|
|
2018-09-10 17:00:34 +00:00
|
|
|
.. code-block:: yaml
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
- model: myapp.person
|
|
|
|
pk: 1
|
|
|
|
fields:
|
|
|
|
first_name: John
|
|
|
|
last_name: Lennon
|
|
|
|
- model: myapp.person
|
2008-08-28 12:49:11 +00:00
|
|
|
pk: 2
|
2008-08-23 22:25:40 +00:00
|
|
|
fields:
|
|
|
|
first_name: Paul
|
|
|
|
last_name: McCartney
|
2008-11-06 10:24:24 +00:00
|
|
|
|
2008-12-07 03:41:23 +00:00
|
|
|
You'll store this data in a ``fixtures`` directory inside your app.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
You can load data by calling :djadmin:`manage.py loaddata <loaddata>`
|
2012-07-15 09:25:13 +00: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 08:11:42 +00:00
|
|
|
from the fixture and reloaded into the database. Note this means that if you
|
2012-07-15 09:25:13 +00: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-23 22:25:40 +00:00
|
|
|
|
2012-02-19 09:02:52 +00: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 17:03:33 +00:00
|
|
|
specify a path to a fixture file, which overrides searching the usual
|
|
|
|
directories.
|
2012-02-19 09:02:52 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Fixtures are also used by the :ref:`testing framework
|
|
|
|
<topics-testing-fixtures>` to help set up a consistent test environment.
|