initial-data.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ======================================
  2. How to provide initial data for models
  3. ======================================
  4. It's sometimes useful to prepopulate your database with hard-coded data when
  5. you're first setting up an app. You can provide initial data with migrations or
  6. fixtures.
  7. Provide initial data with migrations
  8. ====================================
  9. To automatically load initial data for an app, create a
  10. :ref:`data migration <data-migrations>`. Migrations are run when setting up the
  11. test database, so the data will be available there, subject to :ref:`some
  12. limitations <test-case-serialized-rollback>`.
  13. .. _initial-data-via-fixtures:
  14. Provide data with fixtures
  15. ==========================
  16. You can also provide data using :ref:`fixtures <fixtures-explanation>`,
  17. however, this data isn't loaded automatically, except if you use
  18. :attr:`.TransactionTestCase.fixtures`.
  19. A fixture is a collection of data that Django knows how to import into a
  20. database. The most straightforward way of creating a fixture if you've already
  21. got some data is to use the :djadmin:`manage.py dumpdata <dumpdata>` command.
  22. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML
  23. (with PyYAML_ installed) documents. The :doc:`serialization documentation
  24. </topics/serialization>` has more details about each of these supported
  25. :ref:`serialization formats <serialization-formats>`.
  26. .. _PyYAML: https://pyyaml.org/
  27. As an example, though, here's what a fixture for a ``Person`` model might look
  28. like in JSON:
  29. .. code-block:: js
  30. [
  31. {
  32. "model": "myapp.person",
  33. "pk": 1,
  34. "fields": {
  35. "first_name": "John",
  36. "last_name": "Lennon"
  37. }
  38. },
  39. {
  40. "model": "myapp.person",
  41. "pk": 2,
  42. "fields": {
  43. "first_name": "Paul",
  44. "last_name": "McCartney"
  45. }
  46. }
  47. ]
  48. And here's that same fixture as YAML:
  49. .. code-block:: yaml
  50. - model: myapp.person
  51. pk: 1
  52. fields:
  53. first_name: John
  54. last_name: Lennon
  55. - model: myapp.person
  56. pk: 2
  57. fields:
  58. first_name: Paul
  59. last_name: McCartney
  60. You'll store this data in a ``fixtures`` directory inside your app.
  61. You can load data by calling :djadmin:`manage.py loaddata <loaddata>`
  62. ``<fixturename>``, where ``<fixturename>`` is the name of the fixture file
  63. you've created. Each time you run :djadmin:`loaddata`, the data will be read
  64. from the fixture and reloaded into the database. Note this means that if you
  65. change one of the rows created by a fixture and then run :djadmin:`loaddata`
  66. again, you'll wipe out any changes you've made.
  67. Tell Django where to look for fixture files
  68. -------------------------------------------
  69. By default, Django looks for fixtures in the ``fixtures`` directory inside each
  70. app, so the command ``loaddata sample`` will find the file
  71. ``my_app/fixtures/sample.json``. This works with relative paths as well, so
  72. ``loaddata my_app/sample`` will find the file
  73. ``my_app/fixtures/my_app/sample.json``.
  74. Django also looks for fixtures in the list of directories provided in the
  75. :setting:`FIXTURE_DIRS` setting.
  76. To completely prevent default search from happening, use an absolute path to
  77. specify the location of your fixture file, e.g. ``loaddata /path/to/sample``.
  78. .. admonition:: Namespace your fixture files
  79. Django will use the first fixture file it finds whose name matches, so if
  80. you have fixture files with the same name in different applications, you
  81. will be unable to distinguish between them in your ``loaddata`` commands.
  82. The easiest way to avoid this problem is by *namespacing* your fixture
  83. files. That is, by putting them inside a directory named for their
  84. application, as in the relative path example above.
  85. .. seealso::
  86. Fixtures are also used by the :ref:`testing framework
  87. <topics-testing-fixtures>` to help set up a consistent test environment.