{"title":"Providing initial data for models","version":"1.8","locale":"en","docname":"howto/initial-data","url":"/en/1.8/howto/initial-data/","canonical":"https://djangodocs.dev/en/1.8/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.","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. There’s a couple of ways you can have Django\nautomatically create this data: you can provide <a class=\"reference internal\" href=\"#providing-initial-data-with-fixtures\">initial data via fixtures</a>, or\nyou can provide <a class=\"reference internal\" href=\"#providing-initial-sql-data\">initial data as SQL</a>.</p>\n<p>In general, using a fixture is a cleaner method since it’s database-agnostic,\nbut initial SQL is also quite a bit more flexible.</p>\n<section id=\"providing-initial-data-with-fixtures\">\n<span id=\"id1\"></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.8/ref/django-admin/#django-admin-dumpdata\"><code class=\"xref std std-djadmin docutils literal notranslate\">manage.py dumpdata</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.8/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.8/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\">Person</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=\"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>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\">fixtures</code> directory inside your app.</p>\n<p>Loading data is easy: just call <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">manage.py loaddata</code></a>\n<code class=\"docutils literal notranslate\">&lt;fixturename&gt;</code>, where <code class=\"docutils literal notranslate\">&lt;fixturename&gt;</code> is the name of the fixture file\nyou’ve created. Each time you run <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">loaddata</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.8/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">loaddata</code></a>\nagain, you’ll wipe out any changes you’ve made.</p>\n<section id=\"automatically-loading-initial-data-fixtures\">\n<h3>Automatically loading initial data fixtures<a class=\"heading-anchor\" href=\"#automatically-loading-initial-data-fixtures\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"version-note version-deprecated\" data-version=\"1.7\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 1.7</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.7: </span>If an application uses migrations, there is no automatic loading of\nfixtures. Since migrations will be required for applications in Django 1.9,\nthis behavior is considered deprecated. If you want to load initial data\nfor an app, consider doing it in a <a class=\"reference internal\" href=\"/en/1.8/topics/migrations/#data-migrations\"><span class=\"std std-ref\">data migration</span></a>.</p>\n</aside>\n<p>If you create a fixture named <code class=\"docutils literal notranslate\">initial_data.[xml/yaml/json]</code>, that fixture will\nbe loaded every time you run <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a>. This is extremely convenient,\nbut be careful: remember that the data will be refreshed <em>every time</em> you run\n<a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a>. So don’t use <code class=\"docutils literal notranslate\">initial_data</code> for data you’ll want to edit.</p>\n</section>\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\">fixtures</code> directory inside each app for\nfixtures. You can set the <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-FIXTURE_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">FIXTURE_DIRS</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.8/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">manage.py loaddata</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\" role=\"note\">\n<p class=\"admonition-title\">See also</p>\n<p>Fixtures are also used by the <a class=\"reference internal\" href=\"/en/1.8/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-sql-data\">\n<span id=\"initial-sql\"></span><h2>Providing initial SQL data<a class=\"heading-anchor\" href=\"#providing-initial-sql-data\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<aside class=\"version-note version-deprecated\" data-version=\"1.7\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 1.7</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.7: </span>If an application uses migrations, there is no loading of initial SQL data\n(including backend-specific SQL data). Since migrations will be required\nfor applications in Django 1.9, this behavior is considered deprecated.\nIf you want to use initial SQL for an app, consider doing it in a\n<a class=\"reference internal\" href=\"/en/1.8/topics/migrations/#data-migrations\"><span class=\"std std-ref\">data migration</span></a>.</p>\n</aside>\n<p>Django provides a hook for passing the database arbitrary SQL that’s executed\njust after the CREATE TABLE statements when you run <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\">migrate</code></a>. You can\nuse this hook to populate default records, or you could also create SQL\nfunctions, views, triggers, etc.</p>\n<p>The hook is simple: Django just looks for a file called <code class=\"docutils literal notranslate\">sql/&lt;modelname&gt;.sql</code>,\nin your app directory, where <code class=\"docutils literal notranslate\">&lt;modelname&gt;</code> is the model’s name in lowercase.</p>\n<p>So, if you had a <code class=\"docutils literal notranslate\">Person</code> model in an app called <code class=\"docutils literal notranslate\">myapp</code>, you could add\narbitrary SQL to the file <code class=\"docutils literal notranslate\">sql/person.sql</code> inside your <code class=\"docutils literal notranslate\">myapp</code> directory.\nHere’s an example of what the file might contain:</p>\n<div class=\"code-block\" data-language=\"sql\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">SQL</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=\"SQL code\"><code><span class=\"k\">INSERT</span> <span class=\"k\">INTO</span> myapp_person <span class=\"p\">(</span>first_name<span class=\"p\">,</span> last_name<span class=\"p\">)</span> <span class=\"k\">VALUES</span> <span class=\"p\">(</span><span class=\"s1\">&#39;John&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Lennon&#39;</span><span class=\"p\">);</span>\n<span class=\"k\">INSERT</span> <span class=\"k\">INTO</span> myapp_person <span class=\"p\">(</span>first_name<span class=\"p\">,</span> last_name<span class=\"p\">)</span> <span class=\"k\">VALUES</span> <span class=\"p\">(</span><span class=\"s1\">&#39;Paul&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;McCartney&#39;</span><span class=\"p\">);</span>\n</code></pre></div>\n<p>Each SQL file, if given, is expected to contain valid SQL statements\nwhich will insert the desired data (e.g., properly-formatted\n<code class=\"docutils literal notranslate\">INSERT</code> statements separated by semicolons).</p>\n<p>The SQL files are read by the <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-sqlcustom\"><code class=\"xref std std-djadmin docutils literal notranslate\">sqlcustom</code></a> and <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-sqlall\"><code class=\"xref std std-djadmin docutils literal notranslate\">sqlall</code></a>\ncommands in <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/\"><span class=\"doc\">manage.py</span></a>. Refer to the <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/\"><span class=\"doc\">manage.py\ndocumentation</span></a> for more information.</p>\n<p>Note that if you have multiple SQL data files, there’s no guarantee of\nthe order in which they’re executed. The only thing you can assume is\nthat, by the time your custom data files are executed, all the\ndatabase tables already will have been created.</p>\n<aside class=\"admonition-initial-sql-data-and-testing admonition\" role=\"note\">\n<p class=\"admonition-title\">Initial SQL data and testing</p>\n<p>This technique <em>cannot</em> be used to provide initial data for\ntesting purposes. Django’s test framework flushes the contents of\nthe test database after each test; as a result, any data added\nusing the custom SQL hook will be lost.</p>\n<p>If you require data for a test case, you should add it using\neither a <a class=\"reference internal\" href=\"/en/1.8/topics/testing/tools/#topics-testing-fixtures\"><span class=\"std std-ref\">test fixture</span></a>, or\nprogrammatically add it during the <code class=\"docutils literal notranslate\">setUp()</code> of your test case.</p>\n</aside>\n<section id=\"database-backend-specific-sql-data\">\n<h3>Database-backend-specific SQL data<a class=\"heading-anchor\" href=\"#database-backend-specific-sql-data\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>There’s also a hook for backend-specific SQL data. For example, you\ncan have separate initial-data files for PostgreSQL and SQLite. For\neach app, Django looks for a file called\n<code class=\"docutils literal notranslate\">&lt;app_label&gt;/sql/&lt;modelname&gt;.&lt;backend&gt;.sql</code>, where <code class=\"docutils literal notranslate\">&lt;app_label&gt;</code> is\nyour app directory, <code class=\"docutils literal notranslate\">&lt;modelname&gt;</code> is the model’s name in lowercase\nand <code class=\"docutils literal notranslate\">&lt;backend&gt;</code> is the last part of the module name provided for the\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-DATABASE-ENGINE\"><code class=\"xref std std-setting docutils literal notranslate\">ENGINE</code></a> in your settings file (e.g., if you have\ndefined a database with an <a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-DATABASE-ENGINE\"><code class=\"xref std std-setting docutils literal notranslate\">ENGINE</code></a> value of\n<code class=\"docutils literal notranslate\">django.db.backends.sqlite3</code>, Django will look for\n<code class=\"docutils literal notranslate\">&lt;app_label&gt;/sql/&lt;modelname&gt;.sqlite3.sql</code>).</p>\n<p>Backend-specific SQL data is executed before non-backend-specific SQL\ndata. For example, if your app contains the files <code class=\"docutils literal notranslate\">sql/person.sql</code>\nand <code class=\"docutils literal notranslate\">sql/person.sqlite3.sql</code> and you’re installing the app on\nSQLite, Django will execute the contents of\n<code class=\"docutils literal notranslate\">sql/person.sqlite3.sql</code> first, then <code class=\"docutils literal notranslate\">sql/person.sql</code>.</p>\n</section>\n</section>","rootId":"providing-initial-data-for-models","toc":[{"title":"Providing initial data with fixtures","anchor":"providing-initial-data-with-fixtures","children":[{"title":"Automatically loading initial data fixtures","anchor":"automatically-loading-initial-data-fixtures","children":[]},{"title":"Where Django finds fixture files","anchor":"where-django-finds-fixture-files","children":[]}]},{"title":"Providing initial SQL data","anchor":"providing-initial-sql-data","children":[{"title":"Database-backend-specific SQL data","anchor":"database-backend-specific-sql-data","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"“How-to” guides","url":"/en/1.8/howto/"}],"prev":{"docname":"howto/error-reporting","title":"Error reporting","url":"/en/1.8/howto/error-reporting/"},"next":{"docname":"howto/jython","title":"Running Django on Jython","url":"/en/1.8/howto/jython/"},"formats":{"html":"/en/1.8/howto/initial-data/","markdown":"/en/1.8/howto/initial-data.md","json":"/en/1.8/howto/initial-data.json"},"source":"https://github.com/django/django/blob/stable/1.8.x/docs/howto/initial-data.txt","official":"https://docs.djangoproject.com/en/1.8/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"]}