initial-data.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. .. _howto-initial-data:
  2. =================================
  3. Providing initial data for models
  4. =================================
  5. It's sometimes useful to pre-populate your database with hard-coded data when
  6. you're first setting up an app. There's a couple of ways you can have Django
  7. automatically create this data: you can provide `initial data via fixtures`_, or
  8. you can provide `initial data as SQL`_.
  9. In general, using a fixture is a cleaner method since it's database-agnostic,
  10. but initial SQL is also quite a bit more flexible.
  11. .. _initial data as sql: `providing initial sql data`_
  12. .. _initial data via fixtures: `providing initial data with 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` command. Or, you can
  18. write fixtures by hand; fixtures can be written as XML, YAML, or JSON documents.
  19. The :ref:`serialization documentation <topics-serialization>` has more details
  20. 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 created.
  58. Every time you run :djadmin:`loaddata` the data will be read from the fixture
  59. and re-loaded into the database. Note that this means that if you change one of
  60. the rows created by a fixture and then run :djadmin:`loaddata` again you'll
  61. 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. .. seealso::
  69. Fixtures are also used by the :ref:`testing framework
  70. <topics-testing-fixtures>` to help set up a consistent test environment.
  71. .. _initial-sql:
  72. Providing initial SQL data
  73. ==========================
  74. Django provides a hook for passing the database arbitrary SQL that's executed
  75. just after the CREATE TABLE statements when you run :djadmin:`syncdb`. You can
  76. use this hook to populate default records, or you could also create SQL
  77. functions, views, triggers, etc.
  78. The hook is simple: Django just looks for a file called ``sql/<modelname>.sql``,
  79. in your app directory, where ``<modelname>`` is the model's name in lowercase.
  80. So, if you had a ``Person`` model in an app called ``myapp``, you could add
  81. arbitrary SQL to the file ``sql/person.sql`` inside your ``myapp`` directory.
  82. Here's an example of what the file might contain:
  83. .. code-block:: sql
  84. INSERT INTO myapp_person (first_name, last_name) VALUES ('John', 'Lennon');
  85. INSERT INTO myapp_person (first_name, last_name) VALUES ('Paul', 'McCartney');
  86. Each SQL file, if given, is expected to contain valid SQL statements
  87. which will insert the desired data (e.g., properly-formatted
  88. ``INSERT`` statements separated by semicolons).
  89. The SQL files are read by the :djadmin:`sqlcustom`, :djadmin:`sqlreset`,
  90. :djadmin:`sqlall` and :djadmin:`reset` commands in :ref:`manage.py
  91. <ref-django-admin>`. Refer to the :ref:`manage.py documentation
  92. <ref-django-admin>` for more information.
  93. Note that if you have multiple SQL data files, there's no guarantee of the order
  94. in which they're executed. The only thing you can assume is that, by the time
  95. your custom data files are executed, all the database tables already will have
  96. been created.
  97. Database-backend-specific SQL data
  98. ----------------------------------
  99. There's also a hook for backend-specific SQL data. For example, you can have
  100. separate initial-data files for PostgreSQL and MySQL. For each app, Django
  101. looks for a file called ``<appname>/sql/<modelname>.<backend>.sql``, where
  102. ``<appname>`` is your app directory, ``<modelname>`` is the model's name in
  103. lowercase and ``<backend>`` is the value of :setting:`DATABASE_ENGINE` in your
  104. settings file (e.g., ``postgresql``, ``mysql``).
  105. Backend-specific SQL data is executed before non-backend-specific SQL data. For
  106. example, if your app contains the files ``sql/person.sql`` and
  107. ``sql/person.postgresql.sql`` and you're installing the app on PostgreSQL,
  108. Django will execute the contents of ``sql/person.postgresql.sql`` first, then
  109. ``sql/person.sql``.