initial-data.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. There's a couple of ways you can have Django
  6. automatically create this data: you can provide `initial data via fixtures`_, or
  7. you can provide `initial data as SQL`_.
  8. In general, using a fixture is a cleaner method since it's database-agnostic,
  9. but initial SQL is also quite a bit more flexible.
  10. .. _initial data as sql: `providing initial sql data`_
  11. .. _initial data via fixtures: `providing initial data with fixtures`_
  12. .. _initial-data-via-fixtures:
  13. Providing initial data with fixtures
  14. ====================================
  15. A fixture is a collection of data that Django knows how to import into a
  16. database. The most straightforward way of creating a fixture if you've already
  17. got some data is to use the :djadmin:`manage.py dumpdata <dumpdata>` command.
  18. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML
  19. (with PyYAML_ installed) documents. The :doc:`serialization documentation
  20. </topics/serialization>` has more details about each of these supported
  21. :ref:`serialization formats <serialization-formats>`.
  22. .. _PyYAML: http://www.pyyaml.org/
  23. As an example, though, here's what a fixture for a simple ``Person`` model might
  24. look like in JSON:
  25. .. code-block:: js
  26. [
  27. {
  28. "model": "myapp.person",
  29. "pk": 1,
  30. "fields": {
  31. "first_name": "John",
  32. "last_name": "Lennon"
  33. }
  34. },
  35. {
  36. "model": "myapp.person",
  37. "pk": 2,
  38. "fields": {
  39. "first_name": "Paul",
  40. "last_name": "McCartney"
  41. }
  42. }
  43. ]
  44. And here's that same fixture as YAML:
  45. .. code-block:: none
  46. - model: myapp.person
  47. pk: 1
  48. fields:
  49. first_name: John
  50. last_name: Lennon
  51. - model: myapp.person
  52. pk: 2
  53. fields:
  54. first_name: Paul
  55. last_name: McCartney
  56. You'll store this data in a ``fixtures`` directory inside your app.
  57. Loading data is easy: just call :djadmin:`manage.py loaddata <loaddata>`
  58. ``<fixturename>``, where ``<fixturename>`` is the name of the fixture file
  59. you've created. Each time you run :djadmin:`loaddata`, the data will be read
  60. from the fixture and re-loaded into the database. Note this means that if you
  61. change one of the rows created by a fixture and then run :djadmin:`loaddata`
  62. again, you'll wipe out any changes you've made.
  63. Where Django finds fixture files
  64. --------------------------------
  65. By default, Django looks in the ``fixtures`` directory inside each app for
  66. fixtures. You can set the :setting:`FIXTURE_DIRS` setting to a list of
  67. additional directories where Django should look.
  68. When running :djadmin:`manage.py loaddata <loaddata>`, you can also
  69. specify a path to a fixture file, which overrides searching the usual
  70. directories.
  71. .. seealso::
  72. Fixtures are also used by the :ref:`testing framework
  73. <topics-testing-fixtures>` to help set up a consistent test environment.
  74. .. _initial-sql:
  75. Providing initial SQL data
  76. ==========================
  77. .. deprecated:: 1.7
  78. If an application uses migrations, there is no loading of initial SQL data
  79. (including backend-specific SQL data). Since migrations will be required
  80. for applications in Django 1.9, this behavior is considered deprecated.
  81. If you want to use initial SQL for an app, consider doing it in a
  82. :ref:`data migration <data-migrations>`.
  83. Django provides a hook for passing the database arbitrary SQL that's executed
  84. just after the CREATE TABLE statements when you run :djadmin:`migrate`. You can
  85. use this hook to populate default records, or you could also create SQL
  86. functions, views, triggers, etc.
  87. The hook is simple: Django just looks for a file called ``sql/<modelname>.sql``,
  88. in your app directory, where ``<modelname>`` is the model's name in lowercase.
  89. So, if you had a ``Person`` model in an app called ``myapp``, you could add
  90. arbitrary SQL to the file ``sql/person.sql`` inside your ``myapp`` directory.
  91. Here's an example of what the file might contain:
  92. .. code-block:: sql
  93. INSERT INTO myapp_person (first_name, last_name) VALUES ('John', 'Lennon');
  94. INSERT INTO myapp_person (first_name, last_name) VALUES ('Paul', 'McCartney');
  95. Each SQL file, if given, is expected to contain valid SQL statements
  96. which will insert the desired data (e.g., properly-formatted
  97. ``INSERT`` statements separated by semicolons).
  98. The SQL files are read by the :djadmin:`sqlcustom` and :djadmin:`sqlall`
  99. commands in :doc:`manage.py </ref/django-admin>`. Refer to the :doc:`manage.py
  100. documentation </ref/django-admin>` for more information.
  101. Note that if you have multiple SQL data files, there's no guarantee of
  102. the order in which they're executed. The only thing you can assume is
  103. that, by the time your custom data files are executed, all the
  104. database tables already will have been created.
  105. .. admonition:: Initial SQL data and testing
  106. This technique *cannot* be used to provide initial data for
  107. testing purposes. Django's test framework flushes the contents of
  108. the test database after each test; as a result, any data added
  109. using the custom SQL hook will be lost.
  110. If you require data for a test case, you should add it using
  111. either a :ref:`test fixture <topics-testing-fixtures>`, or
  112. programmatically add it during the ``setUp()`` of your test case.
  113. Database-backend-specific SQL data
  114. ----------------------------------
  115. There's also a hook for backend-specific SQL data. For example, you
  116. can have separate initial-data files for PostgreSQL and SQLite. For
  117. each app, Django looks for a file called
  118. ``<app_label>/sql/<modelname>.<backend>.sql``, where ``<app_label>`` is
  119. your app directory, ``<modelname>`` is the model's name in lowercase
  120. and ``<backend>`` is the last part of the module name provided for the
  121. :setting:`ENGINE <DATABASE-ENGINE>` in your settings file (e.g., if you have
  122. defined a database with an :setting:`ENGINE <DATABASE-ENGINE>` value of
  123. ``django.db.backends.sqlite3``, Django will look for
  124. ``<app_label>/sql/<modelname>.sqlite3.sql``).
  125. Backend-specific SQL data is executed before non-backend-specific SQL
  126. data. For example, if your app contains the files ``sql/person.sql``
  127. and ``sql/person.sqlite3.sql`` and you're installing the app on
  128. SQLite, Django will execute the contents of
  129. ``sql/person.sqlite3.sql`` first, then ``sql/person.sql``.