fixtures.txt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. .. _fixtures-explanation:
  2. ========
  3. Fixtures
  4. ========
  5. .. seealso::
  6. * :doc:`/howto/initial-data`
  7. What is a fixture?
  8. ==================
  9. A *fixture* is a collection of files that contain the serialized contents of
  10. the database. Each fixture has a unique name, and the files that comprise the
  11. fixture can be distributed over multiple directories, in multiple applications.
  12. How to produce a fixture?
  13. =========================
  14. Fixtures can be generated by :djadmin:`manage.py dumpdata <dumpdata>`. It's
  15. also possible to generate custom fixtures by directly using
  16. :doc:`serialization documentation </topics/serialization>` tools or even by
  17. handwriting them.
  18. What to use a fixture for?
  19. ==========================
  20. Fixtures can be used to pre-populate database with data for
  21. :ref:`tests <topics-testing-fixtures>` or to provide some :ref:`initial data
  22. <initial-data-via-fixtures>`.
  23. Were Django looks for fixtures?
  24. ===============================
  25. Django will search in three locations for fixtures:
  26. 1. In the ``fixtures`` directory of every installed application
  27. 2. In any directory named in the :setting:`FIXTURE_DIRS` setting
  28. 3. In the literal path named by the fixture
  29. Django will load any and all fixtures it finds in these locations that match
  30. the provided fixture names.
  31. If the named fixture has a file extension, only fixtures of that type
  32. will be loaded. For example:
  33. .. code-block:: shell
  34. django-admin loaddata mydata.json
  35. would only load JSON fixtures called ``mydata``. The fixture extension
  36. must correspond to the registered name of a
  37. :ref:`serializer <serialization-formats>` (e.g., ``json`` or ``xml``).
  38. If you omit the extensions, Django will search all available fixture types
  39. for a matching fixture. For example:
  40. .. code-block:: shell
  41. django-admin loaddata mydata
  42. would look for any fixture of any fixture type called ``mydata``. If a fixture
  43. directory contained ``mydata.json``, that fixture would be loaded
  44. as a JSON fixture.
  45. The fixtures that are named can include directory components. These
  46. directories will be included in the search path. For example:
  47. .. code-block:: shell
  48. django-admin loaddata foo/bar/mydata.json
  49. would search ``<app_label>/fixtures/foo/bar/mydata.json`` for each installed
  50. application, ``<dirname>/foo/bar/mydata.json`` for each directory in
  51. :setting:`FIXTURE_DIRS`, and the literal path ``foo/bar/mydata.json``.
  52. How fixtures are saved to the database?
  53. =======================================
  54. When fixture files are processed, the data is saved to the database as is.
  55. Model defined :meth:`~django.db.models.Model.save` methods are not called, and
  56. any :data:`~django.db.models.signals.pre_save` or
  57. :data:`~django.db.models.signals.post_save` signals will be called with
  58. ``raw=True`` since the instance only contains attributes that are local to the
  59. model. You may, for example, want to disable handlers that access
  60. related fields that aren't present during fixture loading and would otherwise
  61. raise an exception::
  62. from django.db.models.signals import post_save
  63. from .models import MyModel
  64. def my_handler(**kwargs):
  65. # disable the handler during fixture loading
  66. if kwargs['raw']:
  67. return
  68. ...
  69. post_save.connect(my_handler, sender=MyModel)
  70. You could also write a decorator to encapsulate this logic::
  71. from functools import wraps
  72. def disable_for_loaddata(signal_handler):
  73. """
  74. Decorator that turns off signal handlers when loading fixture data.
  75. """
  76. @wraps(signal_handler)
  77. def wrapper(*args, **kwargs):
  78. if kwargs['raw']:
  79. return
  80. signal_handler(*args, **kwargs)
  81. return wrapper
  82. @disable_for_loaddata
  83. def my_handler(**kwargs):
  84. ...
  85. Just be aware that this logic will disable the signals whenever fixtures are
  86. deserialized, not just during ``loaddata``.
  87. Note that the order in which fixture files are processed is undefined. However,
  88. all fixture data is installed as a single transaction, so data in
  89. one fixture can reference data in another fixture. If the database backend
  90. supports row-level constraints, these constraints will be checked at the
  91. end of the transaction.
  92. Compressed fixtures
  93. ===================
  94. Fixtures may be compressed in ``zip``, ``gz``, ``bz2``, ``lzma``, or ``xz``
  95. format. For example:
  96. .. code-block:: shell
  97. django-admin loaddata mydata.json
  98. would look for any of ``mydata.json``, ``mydata.json.zip``, ``mydata.json.gz``,
  99. ``mydata.json.bz2``, ``mydata.json.lzma``, or ``mydata.json.xz``. The first
  100. file contained within a compressed archive is used.
  101. Note that if two fixtures with the same name but different
  102. fixture type are discovered (for example, if ``mydata.json`` and
  103. ``mydata.xml.gz`` were found in the same fixture directory), fixture
  104. installation will be aborted, and any data installed in the call to
  105. ``loaddata`` will be removed from the database.
  106. .. admonition:: MySQL with MyISAM and fixtures
  107. The MyISAM storage engine of MySQL doesn't support transactions or
  108. constraints, so if you use MyISAM, you won't get validation of fixture
  109. data, or a rollback if multiple transaction files are found.
  110. Database-specific fixtures
  111. ==========================
  112. If you're in a multi-database setup, you might have fixture data that
  113. you want to load onto one database, but not onto another. In this
  114. situation, you can add a database identifier into the names of your fixtures.
  115. For example, if your :setting:`DATABASES` setting has a ``users`` database
  116. defined, name the fixture ``mydata.users.json`` or
  117. ``mydata.users.json.gz`` and the fixture will only be loaded when you
  118. specify you want to load data into the ``users`` database.