{"title":"모델에 초기 데이터를 제공하는 방법","version":"5.0","locale":"ko","docname":"howto/initial-data","url":"/ko/5.0/howto/initial-data/","canonical":"https://djangodocs.dev/ko/5.0/howto/initial-data/","summary":"앱을 처음 설정할 때 하드 코딩된 데이터로 데이터베이스를 미리 채우는 것이 때때로 유용합니다.","html":"<h1>모델에 초기 데이터를 제공하는 방법<a class=\"heading-anchor\" href=\"#how-to-provide-initial-data-for-models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>앱을 처음 설정할 때 하드 코딩된 데이터로 데이터베이스를 미리 채우는 것이 때때로 유용합니다. 마이그레이션 또는 고정 장치와 함께 초기 데이터를 제공할 수 있습니다.</p>\n<section id=\"provide-initial-data-with-migrations\">\n<h2>Provide initial data with migrations<a class=\"heading-anchor\" href=\"#provide-initial-data-with-migrations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To automatically load initial data for an app, create a\n<a class=\"reference internal\" href=\"/ko/5.0/topics/migrations/#data-migrations\"><span class=\"std std-ref\">data migration</span></a>. Migrations are run when setting up the\ntest database, so the data will be available there, subject to <a class=\"reference internal\" href=\"/ko/5.0/topics/testing/overview/#test-case-serialized-rollback\"><span class=\"std std-ref\">some\nlimitations</span></a>.</p>\n</section>\n<section id=\"provide-data-with-fixtures\">\n<span id=\"initial-data-via-fixtures\"></span><h2>Provide data with fixtures<a class=\"heading-anchor\" href=\"#provide-data-with-fixtures\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>You can also provide data using <a class=\"reference internal\" href=\"/ko/5.0/topics/db/fixtures/#fixtures-explanation\"><span class=\"std std-ref\">fixtures</span></a>,\nhowever, this data isn’t loaded automatically, except if you use\n<a class=\"reference internal\" href=\"/ko/5.0/topics/testing/tools/#django.test.TransactionTestCase.fixtures\" title=\"django.test.TransactionTestCase.fixtures\"><code class=\"xref py py-attr docutils literal notranslate\">TransactionTestCase.fixtures</code></a>.</p>\n<p>고정 장치는 Django가 데이터베이스로 가져오는 방법을 알고 있는 데이터의 모음입니다. 데이터가 이미 있는 경우 고정장치를 만드는 가장 간단한 방법은 :tembadmin:’manage.py dumpdata’ 명령을 사용하는 것입니다. 또는 수동으로 고정장치를 작성할 수 있으며 고정장치는 JSON, XML 또는 YAML(PyYAML_installed) 문서로 작성할 수 있습니다. :doc:’직렬화 설명서’는 지원되는 각:ref:’직렬화 형식’에 대한 자세한 정보를 제공합니다.</p>\n<p>그러나 한 예로 JSON에서 “사람” 모델의 고정장치는 다음과 같습니다.</p>\n<div class=\"code-block\" data-language=\"js\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Js</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=\"Js code\"><code><span class=\"p\">[</span>\n  <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;model&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;myapp.person&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;pk&quot;</span><span class=\"o\">:</span> <span class=\"mf\">1</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;fields&quot;</span><span class=\"o\">:</span> <span class=\"p\">{</span>\n      <span class=\"s2\">&quot;first_name&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;John&quot;</span><span class=\"p\">,</span>\n      <span class=\"s2\">&quot;last_name&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;Lennon&quot;</span>\n    <span class=\"p\">}</span>\n  <span class=\"p\">},</span>\n  <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;model&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;myapp.person&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;pk&quot;</span><span class=\"o\">:</span> <span class=\"mf\">2</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;fields&quot;</span><span class=\"o\">:</span> <span class=\"p\">{</span>\n      <span class=\"s2\">&quot;first_name&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;Paul&quot;</span><span class=\"p\">,</span>\n      <span class=\"s2\">&quot;last_name&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;McCartney&quot;</span>\n    <span class=\"p\">}</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>그리고 YAML로 된 동일한 fixture 가 있습니다.</p>\n<div class=\"code-block\" data-language=\"yaml\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">YAML</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=\"YAML code\"><code><span class=\"p p-Indicator\">-</span> <span class=\"nt\">model</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">myapp.person</span>\n  <span class=\"nt\">pk</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">1</span>\n  <span class=\"nt\">fields</span><span class=\"p\">:</span>\n    <span class=\"nt\">first_name</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">John</span>\n    <span class=\"nt\">last_name</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">Lennon</span>\n<span class=\"p p-Indicator\">-</span> <span class=\"nt\">model</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">myapp.person</span>\n  <span class=\"nt\">pk</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">2</span>\n  <span class=\"nt\">fields</span><span class=\"p\">:</span>\n    <span class=\"nt\">first_name</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">Paul</span>\n    <span class=\"nt\">last_name</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">McCartney</span>\n</code></pre></div>\n<p>이 데이터는 앱 안에 있는 “고정장치” 디렉토리에 저장됩니다.</p>\n<p>:djadmin:’manage.py loaddata &lt;loaddata&gt;` <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>&lt;fixturename&gt;``를 호출하여 데이터를 로드할 수 있습니다. <a href=\"#id3\"><span class=\"problematic\" id=\"id4\">``</span></a>&lt;fixturename&gt;``는 당신이 만든 fixture 파일의 이름입니다. :djadmin:<a href=\"#id5\"><span class=\"problematic\" id=\"id6\">`</span></a>loaddata`를 실행할 때마다 데이터는 고정장치에서 읽혀져 데이터베이스에 다시 로드됩니다. 이는 고정 장치에 의해 생성된 행 중 하나를 변경한 후 :djadmin:<a href=\"#id7\"><span class=\"problematic\" id=\"id8\">`</span></a>loaddata`를 다시 실행하는 경우, 변경 내용이 모두 지워진다는 의미입니다.</p>\n<section id=\"tell-django-where-to-look-for-fixture-files\">\n<h3>Tell Django where to look for fixture files<a class=\"heading-anchor\" href=\"#tell-django-where-to-look-for-fixture-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>By default, Django looks for fixtures in the <code class=\"docutils literal notranslate\">fixtures</code> directory inside each\napp for, so the command <code class=\"docutils literal notranslate\">loaddata sample</code> will find the file\n<code class=\"docutils literal notranslate\">my_app/fixtures/sample.json</code>. This works with relative paths as well, so\n<code class=\"docutils literal notranslate\">loaddata my_app/sample</code> will find the file\n<code class=\"docutils literal notranslate\">my_app/fixtures/my_app/sample.json</code>.</p>\n<p>Django also looks for fixtures in the list of directories provided in the\n<a class=\"reference internal\" href=\"/ko/5.0/ref/settings/#std-setting-FIXTURE_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">FIXTURE_DIRS</code></a> setting.</p>\n<p>To completely prevent default search form happening, use an absolute path to\nspecify the location of your fixture file, e.g. <code class=\"docutils literal notranslate\">loaddata /path/to/sample</code>.</p>\n<aside class=\"admonition-namespace-your-fixture-files admonition\" role=\"note\">\n<p class=\"admonition-title\">Namespace your fixture files</p>\n<p>Django will use the first fixture file it finds whose name matches, so if\nyou have fixture files with the same name in different applications, you\nwill be unable to distinguish between them in your <code class=\"docutils literal notranslate\">loaddata</code> commands.\nThe easiest way to avoid this problem is by <em>namespacing</em> your fixture\nfiles. That is, by putting them inside a directory named for their\napplication, as in the relative path example above.</p>\n</aside>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">더 보기</p>\n<p>또한 :ref:’testing framework’에서는 고정장치를 사용하여 일관된 테스트 환경을 설정할 수 있습니다.</p>\n</aside>\n</section>\n</section>","rootId":"how-to-provide-initial-data-for-models","toc":[{"title":"Provide initial data with migrations","anchor":"provide-initial-data-with-migrations","children":[]},{"title":"Provide data with fixtures","anchor":"provide-data-with-fixtures","children":[{"title":"Tell Django where to look for fixture files","anchor":"tell-django-where-to-look-for-fixture-files","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"“How-to” 가이드","url":"/ko/5.0/howto/"}],"prev":{"docname":"howto/error-reporting","title":"오류 보고를 관리하는 방법","url":"/ko/5.0/howto/error-reporting/"},"next":{"docname":"howto/legacy-databases","title":"Django를 레거시 데이터베이스와 통합하는 방법","url":"/ko/5.0/howto/legacy-databases/"},"formats":{"html":"/ko/5.0/howto/initial-data/","markdown":"/ko/5.0/howto/initial-data.md","json":"/ko/5.0/howto/initial-data.json"},"source":"https://github.com/django/django/blob/stable/5.0.x/docs/howto/initial-data.txt","official":"https://docs.djangoproject.com/ko/5.0/howto/initial-data/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}