{"title":"モデルに対する初期データを投入する","version":"4.2","locale":"ja","docname":"howto/initial-data","url":"/ja/4.2/howto/initial-data/","canonical":"https://djangodocs.dev/ja/4.2/howto/initial-data/","summary":"It's sometimes useful to prepopulate your database with hard-coded data when you're first setting up an app. You can provide initial data with migrations or…","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>It's sometimes useful to prepopulate your database with hard-coded data when\nyou're first setting up an app. You can provide initial data with migrations or\nfixtures.</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=\"/ja/4.2/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=\"/ja/4.2/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=\"/ja/4.2/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=\"/ja/4.2/topics/testing/tools/#django.test.TransactionTestCase.fixtures\" title=\"django.test.TransactionTestCase.fixtures\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">TransactionTestCase.fixtures</span></code></a>.</p>\n<p>フィクスチャは、Djangoがデータベースにインポートする方法を知っているデータのコレクションです。 すでにいくつかのデータがある場合、フィクスチャを作成する最も簡単な方法は <a class=\"reference internal\" href=\"/ja/4.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> コマンドを使用することです。 もしくは、フィクスチャを手動で書くこともできます; フィクスチャは JSON、XML、または (PyYAMLをインストールして) YAML ドキュメントとして記述できます。 <a class=\"reference internal\" href=\"/ja/4.2/topics/serialization/\"><span class=\"doc\">serialization documentation</span></a> でサポートされている各 <a class=\"reference internal\" href=\"/ja/4.2/topics/serialization/#serialization-formats\"><span class=\"std std-ref\">serialization formats</span></a> の詳細を確認できます。</p>\n<p>As an example, though, here's what a fixture for a <code class=\"docutils literal notranslate\"><span class=\"pre\">Person</span></code> model might look\nlike in 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=\"w\">  </span><span class=\"p\">{</span>\n<span class=\"w\">    </span><span class=\"s2\">&quot;model&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;myapp.person&quot;</span><span class=\"p\">,</span>\n<span class=\"w\">    </span><span class=\"s2\">&quot;pk&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"mf\">1</span><span class=\"p\">,</span>\n<span class=\"w\">    </span><span class=\"s2\">&quot;fields&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"p\">{</span>\n<span class=\"w\">      </span><span class=\"s2\">&quot;first_name&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;John&quot;</span><span class=\"p\">,</span>\n<span class=\"w\">      </span><span class=\"s2\">&quot;last_name&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;Lennon&quot;</span>\n<span class=\"w\">    </span><span class=\"p\">}</span>\n<span class=\"w\">  </span><span class=\"p\">},</span>\n<span class=\"w\">  </span><span class=\"p\">{</span>\n<span class=\"w\">    </span><span class=\"s2\">&quot;model&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;myapp.person&quot;</span><span class=\"p\">,</span>\n<span class=\"w\">    </span><span class=\"s2\">&quot;pk&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"mf\">2</span><span class=\"p\">,</span>\n<span class=\"w\">    </span><span class=\"s2\">&quot;fields&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"p\">{</span>\n<span class=\"w\">      </span><span class=\"s2\">&quot;first_name&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;Paul&quot;</span><span class=\"p\">,</span>\n<span class=\"w\">      </span><span class=\"s2\">&quot;last_name&quot;</span><span class=\"o\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;McCartney&quot;</span>\n<span class=\"w\">    </span><span class=\"p\">}</span>\n<span class=\"w\">  </span><span class=\"p\">}</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>YAML のフィクスチャだと以下のようになります:</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=\"w\"> </span><span class=\"nt\">model</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"l l-Scalar l-Scalar-Plain\">myapp.person</span>\n<span class=\"w\">  </span><span class=\"nt\">pk</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"l l-Scalar l-Scalar-Plain\">1</span>\n<span class=\"w\">  </span><span class=\"nt\">fields</span><span class=\"p\">:</span>\n<span class=\"w\">    </span><span class=\"nt\">first_name</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"l l-Scalar l-Scalar-Plain\">John</span>\n<span class=\"w\">    </span><span class=\"nt\">last_name</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"l l-Scalar l-Scalar-Plain\">Lennon</span>\n<span class=\"p p-Indicator\">-</span><span class=\"w\"> </span><span class=\"nt\">model</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"l l-Scalar l-Scalar-Plain\">myapp.person</span>\n<span class=\"w\">  </span><span class=\"nt\">pk</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"l l-Scalar l-Scalar-Plain\">2</span>\n<span class=\"w\">  </span><span class=\"nt\">fields</span><span class=\"p\">:</span>\n<span class=\"w\">    </span><span class=\"nt\">first_name</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"l l-Scalar l-Scalar-Plain\">Paul</span>\n<span class=\"w\">    </span><span class=\"nt\">last_name</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"l l-Scalar l-Scalar-Plain\">McCartney</span>\n</code></pre></div>\n<p>このデータは、アプリケーション内の <code class=\"docutils literal notranslate\"><span class=\"pre\">fixtures</span></code> ディレクトリ内に保存しておくことができます。</p>\n<p>You can load data by calling <a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">manage.py</span> <span class=\"pre\">loaddata</span></code></a>\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;fixturename&gt;</span></code>, where <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;fixturename&gt;</span></code> is the name of the fixture file\nyou've created. Each time you run <a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">loaddata</span></code></a>, the data will be read\nfrom the fixture and reloaded into the database. Note this means that if you\nchange one of the rows created by a fixture and then run <a class=\"reference internal\" href=\"/ja/4.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">loaddata</span></code></a>\nagain, you'll wipe out any changes you've made.</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\"><span class=\"pre\">fixtures</span></code> directory inside each\napp for, so the command <code class=\"docutils literal notranslate\"><span class=\"pre\">loaddata</span> <span class=\"pre\">sample</span></code> will find the file\n<code class=\"docutils literal notranslate\"><span class=\"pre\">my_app/fixtures/sample.json</span></code>. This works with relative paths as well, so\n<code class=\"docutils literal notranslate\"><span class=\"pre\">loaddata</span> <span class=\"pre\">my_app/sample</span></code> will find the file\n<code class=\"docutils literal notranslate\"><span class=\"pre\">my_app/fixtures/my_app/sample.json</span></code>.</p>\n<p>Django also looks for fixtures in the list of directories provided in the\n<a class=\"reference internal\" href=\"/ja/4.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>\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\"><span class=\"pre\">loaddata</span> <span class=\"pre\">/path/to/sample</span></code>.</p>\n<aside class=\"admonition-namespace-your-fixture-files admonition\">\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\"><span class=\"pre\">loaddata</span></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\">\n<p class=\"admonition-title\">参考</p>\n<p>フィクスチャは <a class=\"reference internal\" href=\"/ja/4.2/topics/testing/tools/#topics-testing-fixtures\"><span class=\"std std-ref\">テストフレームワーク</span></a> でも利用でき、テスト環境の一貫性を保つのに役立ちます。</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":"/ja/4.2/howto/"}],"prev":{"docname":"howto/error-reporting","title":"エラーレポートの管理","url":"/ja/4.2/howto/error-reporting/"},"next":{"docname":"howto/legacy-databases","title":"レガシーなデータベースと Django の統合","url":"/ja/4.2/howto/legacy-databases/"},"formats":{"html":"/ja/4.2/howto/initial-data/","markdown":"/ja/4.2/howto/initial-data.md","json":"/ja/4.2/howto/initial-data.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/howto/initial-data.txt","official":"https://docs.djangoproject.com/ja/4.2/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","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}