2
0

initial-data.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 XML, YAML, or
  19. JSON documents. The :doc:`serialization documentation </topics/serialization>`
  20. has more details about each of these supported :ref:`serialization formats
  21. <serialization-formats>`.
  22. As an example, though, here's what a fixture for a simple ``Person`` model might
  23. look like in JSON:
  24. .. code-block:: js
  25. [
  26. {
  27. "model": "myapp.person",
  28. "pk": 1,
  29. "fields": {
  30. "first_name": "John",
  31. "last_name": "Lennon"
  32. }
  33. },
  34. {
  35. "model": "myapp.person",
  36. "pk": 2,
  37. "fields": {
  38. "first_name": "Paul",
  39. "last_name": "McCartney"
  40. }
  41. }
  42. ]
  43. And here's that same fixture as YAML:
  44. .. code-block:: none
  45. - model: myapp.person
  46. pk: 1
  47. fields:
  48. first_name: John
  49. last_name: Lennon
  50. - model: myapp.person
  51. pk: 2
  52. fields:
  53. first_name: Paul
  54. last_name: McCartney
  55. You'll store this data in a ``fixtures`` directory inside your app.
  56. Loading data is easy: just call :djadmin:`manage.py loaddata <fixturename>
  57. <loaddata>`, where ``<fixturename>`` is the name of the fixture file you've
  58. created. Every time you run :djadmin:`loaddata` the data will be read from the
  59. fixture and re-loaded into the database. Note that this means that if you
  60. change one of the rows created by a fixture and then run :djadmin:`loaddata`
  61. again you'll wipe out any changes you've made.
  62. Automatically loading initial data fixtures
  63. -------------------------------------------
  64. If you create a fixture named ``initial_data.[xml/yaml/json]``, that fixture will
  65. be loaded every time you run :djadmin:`syncdb`. This is extremely convenient,
  66. but be careful: remember that the data will be refreshed *every time* you run
  67. :djadmin:`syncdb`. So don't use ``initial_data`` for data you'll want to edit.
  68. Where Django finds fixture files
  69. --------------------------------
  70. By default, Django looks in the ``fixtures`` directory inside each app for
  71. fixtures. You can set the :setting:`FIXTURE_DIRS` setting to a list of
  72. additional directories where Django should look.
  73. When running :djadmin:`manage.py loaddata <loaddata>`, you can also
  74. specify an absolute path to a fixture file, which overrides searching
  75. the usual directories.
  76. .. seealso::
  77. Fixtures are also used by the :ref:`testing framework
  78. <topics-testing-fixtures>` to help set up a consistent test environment.
  79. .. _initial-sql:
  80. Providing initial SQL data
  81. ==========================
  82. Django provides a hook for passing the database arbitrary SQL that's executed
  83. just after the CREATE TABLE statements when you run :djadmin:`syncdb`. You can
  84. use this hook to populate default records, or you could also create SQL
  85. functions, views, triggers, etc.
  86. The hook is simple: Django just looks for a file called ``sql/<modelname>.sql``,
  87. in your app directory, where ``<modelname>`` is the model's name in lowercase.
  88. So, if you had a ``Person`` model in an app called ``myapp``, you could add
  89. arbitrary SQL to the file ``sql/person.sql`` inside your ``myapp`` directory.
  90. Here's an example of what the file might contain:
  91. .. code-block:: sql
  92. INSERT INTO myapp_person (first_name, last_name) VALUES ('John', 'Lennon');
  93. INSERT INTO myapp_person (first_name, last_name) VALUES ('Paul', 'McCartney');
  94. Each SQL file, if given, is expected to contain valid SQL statements
  95. which will insert the desired data (e.g., properly-formatted
  96. ``INSERT`` statements separated by semicolons).
  97. The SQL files are read by the :djadmin:`sqlcustom`, :djadmin:`sqlreset`,
  98. :djadmin:`sqlall` and :djadmin:`reset` commands in :doc:`manage.py
  99. </ref/django-admin>`. Refer to the :doc:`manage.py documentation
  100. </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. programatically 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. ``<appname>/sql/<modelname>.<backend>.sql``, where ``<appname>`` 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` in your settings file (e.g., if you have defined a
  122. database with an :setting:`ENGINE` value of
  123. ``django.db.backends.sqlite3``, Django will look for
  124. ``<appname>/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.sqlite.sql`` first, then ``sql/person.sql``.