{"title":"픽스처","version":"5.2","locale":"ko","docname":"topics/db/fixtures","url":"/ko/5.2/topics/db/fixtures/","canonical":"https://djangodocs.dev/ko/5.2/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>픽스처<a class=\"heading-anchor\" href=\"#fixtures\"><span class=\"visually-hidden\">Link to this heading</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\">\n<p class=\"admonition-title\">더 보기</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/ko/5.2/howto/initial-data/\"><span class=\"doc\">모델에 초기 데이터를 제공하는 방법</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 to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Fixtures can be generated by <a class=\"reference internal\" href=\"/ko/5.2/ref/django-admin/#django-admin-dumpdata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">manage.py</span> <span class=\"pre\">dumpdata</span></code></a>. It’s\nalso possible to generate custom fixtures by directly using <a class=\"reference internal\" href=\"/ko/5.2/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 to this heading</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=\"/ko/5.2/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=\"w\"> </span><span class=\"nc\">MyTestCase</span><span class=\"p\">(</span><span class=\"n\">TestCase</span><span class=\"p\">):</span>\n    <span class=\"n\">fixtures</span> <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=\"/ko/5.2/howto/initial-data/#initial-data-via-fixtures\"><span class=\"std std-ref\">initial data</span></a> using the\n<a class=\"reference internal\" href=\"/ko/5.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">loaddata</span></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<span class=\"w\"> </span>loaddata<span class=\"w\"> </span>&lt;fixture<span class=\"w\"> </span>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 to this heading</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\"><span class=\"pre\">fixtures</span></code> directory of every installed application</p></li>\n<li><p>In any directory listed in the <a class=\"reference internal\" href=\"/ko/5.2/ref/settings/#std-setting-FIXTURE_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FIXTURE_DIRS</span></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<span class=\"w\"> </span>loaddata<span class=\"w\"> </span>mydata.json\n</code></pre></div>\n<p>would only load JSON fixtures called <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata</span></code>. The fixture extension must\ncorrespond to the registered name of a\n<a class=\"reference internal\" href=\"/ko/5.2/topics/serialization/#serialization-formats\"><span class=\"std std-ref\">serializer</span></a> (e.g., <code class=\"docutils literal notranslate\"><span class=\"pre\">json</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">xml</span></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<span class=\"w\"> </span>loaddata<span class=\"w\"> </span>mydata\n</code></pre></div>\n<p>would look for any fixture of any fixture type called <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata</span></code>. If a fixture\ndirectory contained <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.json</span></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<span class=\"w\"> </span>loaddata<span class=\"w\"> </span>foo/bar/mydata.json\n</code></pre></div>\n<p>would search <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;app_label&gt;/fixtures/foo/bar/mydata.json</span></code> for each installed\napplication, <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;dirname&gt;/foo/bar/mydata.json</span></code> for each directory in\n<a class=\"reference internal\" href=\"/ko/5.2/ref/settings/#std-setting-FIXTURE_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FIXTURE_DIRS</span></code></a>, and the literal path <code class=\"docutils literal notranslate\"><span class=\"pre\">foo/bar/mydata.json</span></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 to this heading</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<span class=\"w\"> </span>loaddata<span class=\"w\"> </span>mammals<span class=\"w\"> </span>birds<span class=\"w\"> </span>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=\"w\"> </span><span class=\"nc\">AnimalTestCase</span><span class=\"p\">(</span><span class=\"n\">TestCase</span><span class=\"p\">):</span>\n    <span class=\"n\">fixtures</span> <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\"><span class=\"pre\">mammals</span></code> from all applications (in\nthe order in which applications are defined in <a class=\"reference internal\" href=\"/ko/5.2/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a>) will\nbe loaded first. Subsequently, all the <code class=\"docutils literal notranslate\"><span class=\"pre\">birds</span></code> fixtures will be loaded,\nfollowed by all the <code class=\"docutils literal notranslate\"><span class=\"pre\">insects</span></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 to this heading</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=\"/ko/5.2/ref/models/instances/#django.db.models.Model.save\" title=\"django.db.models.Model.save\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">save()</span></code></a> methods are not called, and\nany <a class=\"reference internal\" href=\"/ko/5.2/ref/signals/#django.db.models.signals.pre_save\" title=\"django.db.models.signals.pre_save\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">pre_save</span></code></a> or\n<a class=\"reference internal\" href=\"/ko/5.2/ref/signals/#django.db.models.signals.post_save\" title=\"django.db.models.signals.post_save\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">post_save</span></code></a> signals will be called with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">raw=True</span></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=\"w\"> </span><span class=\"nn\">django.db.models.signals</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">post_save</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">MyModel</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_handler</span><span class=\"p\">(</span><span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n    <span class=\"c1\"># disable the handler during fixture loading</span>\n    <span class=\"k\">if</span> <span class=\"n\">kwargs</span><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\n<span class=\"n\">post_save</span><span class=\"o\">.</span><span class=\"n\">connect</span><span class=\"p\">(</span><span class=\"n\">my_handler</span><span class=\"p\">,</span> <span class=\"n\">sender</span><span class=\"o\">=</span><span class=\"n\">MyModel</span><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=\"w\"> </span><span class=\"nn\">functools</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">wraps</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">disable_for_loaddata</span><span class=\"p\">(</span><span class=\"n\">signal_handler</span><span class=\"p\">):</span>\n<span class=\"w\">    </span><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><span class=\"n\">signal_handler</span><span class=\"p\">)</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">wrapper</span><span class=\"p\">(</span><span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">):</span>\n        <span class=\"k\">if</span> <span class=\"n\">kwargs</span><span class=\"p\">[</span><span class=\"s2\">&quot;raw&quot;</span><span class=\"p\">]:</span>\n            <span class=\"k\">return</span>\n        <span class=\"n\">signal_handler</span><span class=\"p\">(</span><span class=\"o\">*</span><span class=\"n\">args</span><span class=\"p\">,</span> <span class=\"o\">**</span><span class=\"n\">kwargs</span><span class=\"p\">)</span>\n\n    <span class=\"k\">return</span> <span class=\"n\">wrapper</span>\n\n\n<span class=\"nd\">@disable_for_loaddata</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_handler</span><span class=\"p\">(</span><span class=\"o\">**</span><span class=\"n\">kwargs</span><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=\"/ko/5.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">loaddata</span></code></a>.</p>\n</section>\n<section id=\"compressed-fixtures\">\n<h2>Compressed fixtures<a class=\"heading-anchor\" href=\"#compressed-fixtures\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Fixtures may be compressed in <code class=\"docutils literal notranslate\"><span class=\"pre\">zip</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">gz</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">bz2</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">lzma</span></code>, or <code class=\"docutils literal notranslate\"><span class=\"pre\">xz</span></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<span class=\"w\"> </span>loaddata<span class=\"w\"> </span>mydata.json\n</code></pre></div>\n<p>would look for any of <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.json</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.json.zip</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.json.gz</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.json.bz2</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.json.lzma</span></code>, or <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.json.xz</span></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\"><span class=\"pre\">mydata.json</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.xml.gz</span></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=\"/ko/5.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">loaddata</span></code></a> will be removed from the database.</p>\n<aside class=\"admonition-mysql-with-myisam-and-fixtures admonition\">\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 to this heading</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=\"/ko/5.2/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> setting has a <code class=\"docutils literal notranslate\"><span class=\"pre\">users</span></code> database\ndefined, name the fixture <code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.users.json</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">mydata.users.json.gz</span></code> and the fixture will only be loaded when you\nspecify you want to load data into the <code class=\"docutils literal notranslate\"><span class=\"pre\">users</span></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":"Compressed fixtures","anchor":"compressed-fixtures","children":[]},{"title":"Database-specific fixtures","anchor":"database-specific-fixtures","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Django 사용하기","url":"/ko/5.2/topics/"},{"docname":"topics/db/index","title":"모델과 데이터베이스","url":"/ko/5.2/topics/db/"}],"prev":{"docname":"topics/db/instrumentation","title":"Database instrumentation","url":"/ko/5.2/topics/db/instrumentation/"},"next":{"docname":"topics/db/examples/index","title":"Examples of model relationship API usage","url":"/ko/5.2/topics/db/examples/"},"formats":{"html":"/ko/5.2/topics/db/fixtures/","markdown":"/ko/5.2/topics/db/fixtures.md","json":"/ko/5.2/topics/db/fixtures.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/topics/db/fixtures.txt","official":"https://docs.djangoproject.com/ko/5.2/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"]}