{"title":"Providing initial data for models","version":"1.10","locale":"en","docname":"howto/initial-data","url":"/en/1.10/howto/initial-data/","canonical":"https://djangodocs.dev/en/1.10/howto/initial-data/","summary":"It’s sometimes useful to pre-populate your database with hard-coded data when you’re first setting up an app. You can provide initial data with fixtures or…","html":"<h1>Providing initial data for models<a class=\"heading-anchor\" href=\"#providing-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 pre-populate your database with hard-coded data when\nyou’re first setting up an app. You can provide initial data with fixtures or\nmigrations.</p>\n<section id=\"providing-initial-data-with-fixtures\">\n<span id=\"initial-data-via-fixtures\"></span><h2>Providing initial data with fixtures<a class=\"heading-anchor\" href=\"#providing-initial-data-with-fixtures\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A fixture is a collection of data that Django knows how to import into a\ndatabase. The most straightforward way of creating a fixture if you’ve already\ngot some data is to use the <a class=\"reference internal\" href=\"/en/1.10/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> command.\nOr, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML\n(with <a class=\"reference external\" href=\"http://www.pyyaml.org/\">PyYAML</a> installed) documents. The <a class=\"reference internal\" href=\"/en/1.10/topics/serialization/\"><span class=\"doc\">serialization documentation</span></a> has more details about each of these supported\n<a class=\"reference internal\" href=\"/en/1.10/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 simple <code class=\"docutils literal notranslate\"><span class=\"pre\">Person</span></code> model might\nlook like 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>And here’s that same fixture as YAML:</p>\n<div class=\"code-block\" data-language=\"none\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">None</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=\"None code\"><code>- model: myapp.person\n  pk: 1\n  fields:\n    first_name: John\n    last_name: Lennon\n- model: myapp.person\n  pk: 2\n  fields:\n    first_name: Paul\n    last_name: McCartney\n</code></pre></div>\n<p>You’ll store this data in a <code class=\"docutils literal notranslate\"><span class=\"pre\">fixtures</span></code> directory inside your app.</p>\n<p>Loading data is easy: just call <a class=\"reference internal\" href=\"/en/1.10/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=\"/en/1.10/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 re-loaded 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=\"/en/1.10/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=\"where-django-finds-fixture-files\">\n<h3>Where Django finds fixture files<a class=\"heading-anchor\" href=\"#where-django-finds-fixture-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>By default, Django looks in the <code class=\"docutils literal notranslate\"><span class=\"pre\">fixtures</span></code> directory inside each app for\nfixtures. You can set the <a class=\"reference internal\" href=\"/en/1.10/ref/settings/#std-setting-FIXTURE_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">FIXTURE_DIRS</span></code></a> setting to a list of\nadditional directories where Django should look.</p>\n<p>When running <a class=\"reference internal\" href=\"/en/1.10/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>, you can also\nspecify a path to a fixture file, which overrides searching the usual\ndirectories.</p>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">See also</p>\n<p>Fixtures are also used by the <a class=\"reference internal\" href=\"/en/1.10/topics/testing/tools/#topics-testing-fixtures\"><span class=\"std std-ref\">testing framework</span></a> to help set up a consistent test environment.</p>\n</aside>\n</section>\n</section>\n<section id=\"providing-initial-data-with-migrations\">\n<h2>Providing initial data with migrations<a class=\"heading-anchor\" href=\"#providing-initial-data-with-migrations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you want to automatically load initial data for an app, don’t use fixtures.\nInstead, create a migration for your application with\n<a class=\"reference internal\" href=\"/en/1.10/ref/migration-operations/#django.db.migrations.operations.RunPython\" title=\"django.db.migrations.operations.RunPython\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunPython</span></code></a> or\n<a class=\"reference internal\" href=\"/en/1.10/ref/migration-operations/#django.db.migrations.operations.RunSQL\" title=\"django.db.migrations.operations.RunSQL\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunSQL</span></code></a> operations.</p>\n</section>","rootId":"providing-initial-data-for-models","toc":[{"title":"Providing initial data with fixtures","anchor":"providing-initial-data-with-fixtures","children":[{"title":"Where Django finds fixture files","anchor":"where-django-finds-fixture-files","children":[]}]},{"title":"Providing initial data with migrations","anchor":"providing-initial-data-with-migrations","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"“How-to” guides","url":"/en/1.10/howto/"}],"prev":{"docname":"howto/error-reporting","title":"Error reporting","url":"/en/1.10/howto/error-reporting/"},"next":{"docname":"howto/jython","title":"Running Django on Jython","url":"/en/1.10/howto/jython/"},"formats":{"html":"/en/1.10/howto/initial-data/","markdown":"/en/1.10/howto/initial-data.md","json":"/en/1.10/howto/initial-data.json"},"source":"https://github.com/django/django/blob/stable/1.10.x/docs/howto/initial-data.txt","official":"https://docs.djangoproject.com/en/1.10/howto/initial-data/","inVersions":["dev","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","1.8"],"inLocales":["en","fr","ja","id","pt-br","es","el","pl"]}