initial-data.txt 2.6 KB

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