{"title":"Luminárias","version":"6.1","locale":"pt-br","docname":"topics/db/fixtures","url":"/pt-br/6.1/topics/db/fixtures/","canonical":"https://djangodocs.dev/pt-br/6.1/topics/db/fixtures/","summary":"A fixture is a collection of files that contain the serialized contents of the database. Each fixture has a unique name, and the files that comprise the fixture can…","html":"<span id=\"fixtures-explanation\"></span><h1>Luminárias<a class=\"heading-anchor\" href=\"#fixtures\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>A <em>fixture</em> is a collection of files that contain the serialized contents of\nthe database. Each fixture has a unique name, and the files that comprise the\nfixture can be distributed over multiple directories, in multiple applications.</p>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">Ver também</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/pt-br/6.1/howto/initial-data/\"><span class=\"doc\">How to provide initial data for models</span></a></p></li>\n</ul>\n</aside>\n<section id=\"how-to-produce-a-fixture\">\n<h2>How to produce a fixture<a class=\"heading-anchor\" href=\"#how-to-produce-a-fixture\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Fixtures can be generated by <a class=\"reference internal\" href=\"/pt-br/6.1/ref/django-admin/#django-admin-dumpdata\"><code class=\"xref std std-djadmin docutils literal notranslate\">manage.py dumpdata</code></a>. It’s\nalso possible to generate custom fixtures by directly using <a class=\"reference internal\" href=\"/pt-br/6.1/topics/serialization/\"><span class=\"doc\">serialization\ntools</span></a> or even by handwriting them.</p>\n</section>\n<section id=\"how-to-use-a-fixture\">\n<h2>How to use a fixture<a class=\"heading-anchor\" href=\"#how-to-use-a-fixture\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Fixtures can be used to pre-populate the database with data for\n<a class=\"reference internal\" href=\"/pt-br/6.1/topics/testing/tools/#topics-testing-fixtures\"><span class=\"std std-ref\">tests</span></a>:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">class</span> <span class=\"nc\">MyTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    fixtures <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;fixture-label&quot;</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>or to provide some <a class=\"reference internal\" href=\"/pt-br/6.1/howto/initial-data/#initial-data-via-fixtures\"><span class=\"std std-ref\">initial data</span></a> using the\n<a class=\"reference internal\" href=\"/pt-br/6.1/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">loaddata</code></a> command:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>django-admin loaddata &lt;fixture label&gt;\n</code></pre></div>\n</section>\n<section id=\"how-fixtures-are-discovered\">\n<h2>How fixtures are discovered<a class=\"heading-anchor\" href=\"#how-fixtures-are-discovered\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django will search in these locations for fixtures:</p>\n<ol class=\"arabic simple\">\n<li><p>In the <code class=\"docutils literal notranslate\">fixtures</code> directory of every installed application</p></li>\n<li><p>In any directory listed in the <a class=\"reference internal\" href=\"/pt-br/6.1/ref/settings/#std-setting-FIXTURE_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">FIXTURE_DIRS</code></a> setting</p></li>\n<li><p>In the literal path named by the fixture</p></li>\n</ol>\n<p>Django will load any and all fixtures it finds in these locations that match\nthe provided fixture names. If the named fixture has a file extension, only\nfixtures of that type will be loaded. For example:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>django-admin loaddata mydata.json\n</code></pre></div>\n<p>would only load JSON fixtures called <code class=\"docutils literal notranslate\">mydata</code>. The fixture extension must\ncorrespond to the registered name of a\n<a class=\"reference internal\" href=\"/pt-br/6.1/topics/serialization/#serialization-formats\"><span class=\"std std-ref\">serializer</span></a> (e.g., <code class=\"docutils literal notranslate\">json</code> or <code class=\"docutils literal notranslate\">xml</code>).</p>\n<p>If you omit the extensions, Django will search all available fixture types for\na matching fixture. For example:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>django-admin loaddata mydata\n</code></pre></div>\n<p>would look for any fixture of any fixture type called <code class=\"docutils literal notranslate\">mydata</code>. If a fixture\ndirectory contained <code class=\"docutils literal notranslate\">mydata.json</code>, that fixture would be loaded as a JSON\nfixture.</p>\n<p>The fixtures that are named can include directory components. These directories\nwill be included in the search path. For example:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>django-admin loaddata foo/bar/mydata.json\n</code></pre></div>\n<p>would search <code class=\"docutils literal notranslate\">&lt;app_label&gt;/fixtures/foo/bar/mydata.json</code> for each installed\napplication, <code class=\"docutils literal notranslate\">&lt;dirname&gt;/foo/bar/mydata.json</code> for each directory in\n<a class=\"reference internal\" href=\"/pt-br/6.1/ref/settings/#std-setting-FIXTURE_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">FIXTURE_DIRS</code></a>, and the literal path <code class=\"docutils literal notranslate\">foo/bar/mydata.json</code>.</p>\n<section id=\"fixtures-loading-order\">\n<h3>Fixtures loading order<a class=\"heading-anchor\" href=\"#fixtures-loading-order\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Multiple fixtures can be specified in the same invocation. For example:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>django-admin loaddata mammals birds insects\n</code></pre></div>\n<p>or in a test case class:</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"k\">class</span> <span class=\"nc\">AnimalTestCase</span><span class=\"p\">(</span>TestCase<span class=\"p\">):</span>\n    fixtures <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;mammals&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;birds&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;insects&quot;</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>The order in which fixtures are loaded follows the order in which they are\nlisted, whether it’s when using the management command or when listing them in\nthe test case class as shown above.</p>\n<p>In these examples, all the fixtures named <code class=\"docutils literal notranslate\">mammals</code> from all applications (in\nthe order in which applications are defined in <a class=\"reference internal\" href=\"/pt-br/6.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\">INSTALLED_APPS</code></a>) will\nbe loaded first. Subsequently, all the <code class=\"docutils literal notranslate\">birds</code> fixtures will be loaded,\nfollowed by all the <code class=\"docutils literal notranslate\">insects</code> fixtures.</p>\n<p>Be aware that if the database backend supports row-level constraints, these\nconstraints will be checked at the end of the transaction. Any relationships\nacross fixtures may result in a load error if the database configuration does\nnot support deferred constraint checking (refer to the <a class=\"reference external\" href=\"https://dev.mysql.com/doc/refman/en/constraint-foreign-key.html\">MySQL</a> docs for an\nexample).</p>\n</section>\n</section>\n<section id=\"how-fixtures-are-saved-to-the-database\">\n<h2>How fixtures are saved to the database<a class=\"heading-anchor\" href=\"#how-fixtures-are-saved-to-the-database\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When fixture files are processed, the data is saved to the database as is.\nModel defined <a class=\"reference internal\" href=\"/pt-br/6.1/ref/models/instances/#django.db.models.Model.save\" title=\"django.db.models.Model.save\"><code class=\"xref py py-meth docutils literal notranslate\">save()</code></a> methods are not called, and\nany <a class=\"reference internal\" href=\"/pt-br/6.1/ref/signals/#django.db.models.signals.pre_save\" title=\"django.db.models.signals.pre_save\"><code class=\"xref py py-data docutils literal notranslate\">pre_save</code></a>,\n<a class=\"reference internal\" href=\"/pt-br/6.1/ref/signals/#django.db.models.signals.post_save\" title=\"django.db.models.signals.post_save\"><code class=\"xref py py-data docutils literal notranslate\">post_save</code></a>, or\n<a class=\"reference internal\" href=\"/pt-br/6.1/ref/signals/#django.db.models.signals.m2m_changed\" title=\"django.db.models.signals.m2m_changed\"><code class=\"xref py py-data docutils literal notranslate\">m2m_changed</code></a> signals will be called with\n<code class=\"docutils literal notranslate\">raw=True</code> since the instance only contains attributes that are local to the\nmodel. You may, for example, want to disable handlers that access\nrelated fields that aren’t present during fixture loading and would otherwise\nraise an exception:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.db.models.signals</span> <span class=\"kn\">import</span> post_save\n<span class=\"kn\">from</span> <span class=\"nn\">.models</span> <span class=\"kn\">import</span> MyModel\n\n\n<span class=\"k\">def</span> <span class=\"nf\">my_handler</span><span class=\"p\">(</span><span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n    <span class=\"c1\"># disable the handler during fixture loading</span>\n    <span class=\"k\">if</span> kwargs<span class=\"p\">[</span><span class=\"s2\">&quot;raw&quot;</span><span class=\"p\">]:</span>\n        <span class=\"k\">return</span>\n    <span class=\"o\">...</span>\n\n\npost_save<span class=\"o\">.</span>connect<span class=\"p\">(</span>my_handler<span class=\"p\">,</span> sender<span class=\"o\">=</span>MyModel<span class=\"p\">)</span>\n</code></pre></div>\n<p>You could also write a decorator to encapsulate this logic:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">functools</span> <span class=\"kn\">import</span> wraps\n\n\n<span class=\"k\">def</span> <span class=\"nf\">disable_for_loaddata</span><span class=\"p\">(</span>signal_handler<span class=\"p\">):</span>\n    <span class=\"sd\">&quot;&quot;&quot;</span>\n<span class=\"sd\">    Decorator that turns off signal handlers when loading fixture data.</span>\n<span class=\"sd\">    &quot;&quot;&quot;</span>\n\n    <span class=\"nd\">@wraps</span><span class=\"p\">(</span>signal_handler<span class=\"p\">)</span>\n    <span class=\"k\">def</span> <span class=\"nf\">wrapper</span><span class=\"p\">(</span><span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">):</span>\n        <span class=\"k\">if</span> kwargs<span class=\"p\">[</span><span class=\"s2\">&quot;raw&quot;</span><span class=\"p\">]:</span>\n            <span class=\"k\">return</span>\n        signal_handler<span class=\"p\">(</span><span class=\"o\">*</span>args<span class=\"p\">,</span> <span class=\"o\">**</span>kwargs<span class=\"p\">)</span>\n\n    <span class=\"k\">return</span> wrapper\n\n\n<span class=\"nd\">@disable_for_loaddata</span>\n<span class=\"k\">def</span> <span class=\"nf\">my_handler</span><span class=\"p\">(</span><span class=\"o\">**</span>kwargs<span class=\"p\">):</span> <span class=\"o\">...</span>\n</code></pre></div>\n<p>Just be aware that this logic will disable the signals whenever fixtures are\ndeserialized, not just during <a class=\"reference internal\" href=\"/pt-br/6.1/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">loaddata</code></a>.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.1\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>The <code class=\"docutils literal notranslate\">raw</code> argument was added to <code class=\"docutils literal notranslate\">m2m_changed</code> signals.</p>\n</aside>\n</section>\n<section id=\"compressed-fixtures\">\n<h2>Fixtures compressas<a class=\"heading-anchor\" href=\"#compressed-fixtures\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Fixtures may be compressed in <code class=\"docutils literal notranslate\">zip</code>, <code class=\"docutils literal notranslate\">gz</code>, <code class=\"docutils literal notranslate\">bz2</code>, <code class=\"docutils literal notranslate\">lzma</code>, or <code class=\"docutils literal notranslate\">xz</code>\nformat. For example:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>django-admin loaddata mydata.json\n</code></pre></div>\n<p>would look for any of <code class=\"docutils literal notranslate\">mydata.json</code>, <code class=\"docutils literal notranslate\">mydata.json.zip</code>, <code class=\"docutils literal notranslate\">mydata.json.gz</code>,\n<code class=\"docutils literal notranslate\">mydata.json.bz2</code>, <code class=\"docutils literal notranslate\">mydata.json.lzma</code>, or <code class=\"docutils literal notranslate\">mydata.json.xz</code>. The first\nfile contained within a compressed archive is used.</p>\n<p>Note that if two fixtures with the same name but different fixture type are\ndiscovered (for example, if <code class=\"docutils literal notranslate\">mydata.json</code> and <code class=\"docutils literal notranslate\">mydata.xml.gz</code> were found in\nthe same fixture directory), fixture installation will be aborted, and any data\ninstalled in the call to <a class=\"reference internal\" href=\"/pt-br/6.1/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">loaddata</code></a> will be removed from the database.</p>\n<aside class=\"admonition-mysql-with-myisam-and-fixtures admonition\" role=\"note\">\n<p class=\"admonition-title\">MySQL with MyISAM and fixtures</p>\n<p>The MyISAM storage engine of MySQL doesn’t support transactions or\nconstraints, so if you use MyISAM, you won’t get validation of fixture\ndata, or a rollback if multiple transaction files are found.</p>\n</aside>\n</section>\n<section id=\"database-specific-fixtures\">\n<h2>Database-specific fixtures<a class=\"heading-anchor\" href=\"#database-specific-fixtures\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you’re in a multi-database setup, you might have fixture data that\nyou want to load onto one database, but not onto another. In this\nsituation, you can add a database identifier into the names of your fixtures.</p>\n<p>For example, if your <a class=\"reference internal\" href=\"/pt-br/6.1/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a> setting has a <code class=\"docutils literal notranslate\">users</code> database\ndefined, name the fixture <code class=\"docutils literal notranslate\">mydata.users.json</code> or\n<code class=\"docutils literal notranslate\">mydata.users.json.gz</code> and the fixture will only be loaded when you\nspecify you want to load data into the <code class=\"docutils literal notranslate\">users</code> database.</p>\n</section>","rootId":"fixtures","toc":[{"title":"How to produce a fixture","anchor":"how-to-produce-a-fixture","children":[]},{"title":"How to use a fixture","anchor":"how-to-use-a-fixture","children":[]},{"title":"How fixtures are discovered","anchor":"how-fixtures-are-discovered","children":[{"title":"Fixtures loading order","anchor":"fixtures-loading-order","children":[]}]},{"title":"How fixtures are saved to the database","anchor":"how-fixtures-are-saved-to-the-database","children":[]},{"title":"Fixtures compressas","anchor":"compressed-fixtures","children":[]},{"title":"Database-specific fixtures","anchor":"database-specific-fixtures","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Usando o Django","url":"/pt-br/6.1/topics/"},{"docname":"topics/db/index","title":"Modelos e bancos de dados","url":"/pt-br/6.1/topics/db/"}],"prev":{"docname":"topics/db/instrumentation","title":"Database instrumentation","url":"/pt-br/6.1/topics/db/instrumentation/"},"next":{"docname":"topics/db/examples/index","title":"Examples of model relationship API usage","url":"/pt-br/6.1/topics/db/examples/"},"formats":{"html":"/pt-br/6.1/topics/db/fixtures/","markdown":"/pt-br/6.1/topics/db/fixtures.md","json":"/pt-br/6.1/topics/db/fixtures.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/topics/db/fixtures.txt","official":"https://docs.djangoproject.com/pt-br/6.1/topics/db/fixtures/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}