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