initial-data.txt 3.0 KB

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