fixtures.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. django-admin loaddata mydata.json
  34. would only load JSON fixtures called ``mydata``. The fixture extension
  35. must correspond to the registered name of a
  36. :ref:`serializer <serialization-formats>` (e.g., ``json`` or ``xml``).
  37. If you omit the extensions, Django will search all available fixture types
  38. for a matching fixture. For example::
  39. django-admin loaddata mydata
  40. would look for any fixture of any fixture type called ``mydata``. If a fixture
  41. directory contained ``mydata.json``, that fixture would be loaded
  42. as a JSON fixture.
  43. The fixtures that are named can include directory components. These
  44. directories will be included in the search path. For example::
  45. django-admin loaddata foo/bar/mydata.json
  46. would search ``<app_label>/fixtures/foo/bar/mydata.json`` for each installed
  47. application, ``<dirname>/foo/bar/mydata.json`` for each directory in
  48. :setting:`FIXTURE_DIRS`, and the literal path ``foo/bar/mydata.json``.
  49. How fixtures are saved to the database?
  50. =======================================
  51. When fixture files are processed, the data is saved to the database as is.
  52. Model defined :meth:`~django.db.models.Model.save` methods are not called, and
  53. any :data:`~django.db.models.signals.pre_save` or
  54. :data:`~django.db.models.signals.post_save` signals will be called with
  55. ``raw=True`` since the instance only contains attributes that are local to the
  56. model. You may, for example, want to disable handlers that access
  57. related fields that aren't present during fixture loading and would otherwise
  58. raise an exception::
  59. from django.db.models.signals import post_save
  60. from .models import MyModel
  61. def my_handler(**kwargs):
  62. # disable the handler during fixture loading
  63. if kwargs['raw']:
  64. return
  65. ...
  66. post_save.connect(my_handler, sender=MyModel)
  67. You could also write a decorator to encapsulate this logic::
  68. from functools import wraps
  69. def disable_for_loaddata(signal_handler):
  70. """
  71. Decorator that turns off signal handlers when loading fixture data.
  72. """
  73. @wraps(signal_handler)
  74. def wrapper(*args, **kwargs):
  75. if kwargs['raw']:
  76. return
  77. signal_handler(*args, **kwargs)
  78. return wrapper
  79. @disable_for_loaddata
  80. def my_handler(**kwargs):
  81. ...
  82. Just be aware that this logic will disable the signals whenever fixtures are
  83. deserialized, not just during ``loaddata``.
  84. Note that the order in which fixture files are processed is undefined. However,
  85. all fixture data is installed as a single transaction, so data in
  86. one fixture can reference data in another fixture. If the database backend
  87. supports row-level constraints, these constraints will be checked at the
  88. end of the transaction.
  89. Compressed fixtures
  90. ===================
  91. Fixtures may be compressed in ``zip``, ``gz``, ``bz2``, ``lzma``, or ``xz``
  92. format. For example::
  93. django-admin loaddata mydata.json
  94. would look for any of ``mydata.json``, ``mydata.json.zip``, ``mydata.json.gz``,
  95. ``mydata.json.bz2``, ``mydata.json.lzma``, or ``mydata.json.xz``. The first
  96. file contained within a compressed archive is used.
  97. Note that if two fixtures with the same name but different
  98. fixture type are discovered (for example, if ``mydata.json`` and
  99. ``mydata.xml.gz`` were found in the same fixture directory), fixture
  100. installation will be aborted, and any data installed in the call to
  101. ``loaddata`` will be removed from the database.
  102. .. admonition:: MySQL with MyISAM and fixtures
  103. The MyISAM storage engine of MySQL doesn't support transactions or
  104. constraints, so if you use MyISAM, you won't get validation of fixture
  105. data, or a rollback if multiple transaction files are found.
  106. Database-specific fixtures
  107. ==========================
  108. If you're in a multi-database setup, you might have fixture data that
  109. you want to load onto one database, but not onto another. In this
  110. situation, you can add a database identifier into the names of your fixtures.
  111. For example, if your :setting:`DATABASES` setting has a ``users`` database
  112. defined, name the fixture ``mydata.users.json`` or
  113. ``mydata.users.json.gz`` and the fixture will only be loaded when you
  114. specify you want to load data into the ``users`` database.