{"title":"How to provide initial data for models","version":"5.0","locale":"pl","docname":"howto/initial-data","url":"/pl/5.0/howto/initial-data/","canonical":"https://djangodocs.dev/pl/5.0/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>How to provide initial data for models<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=\"/pl/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=\"/pl/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=\"/pl/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=\"/pl/5.0/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>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=\"/pl/5.0/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=\"https://pyyaml.org/\">PyYAML</a> installed) documents. The <a class=\"reference internal\" href=\"/pl/5.0/topics/serialization/\"><span class=\"doc\">serialization documentation</span></a> has more details about each of these supported\n<a class=\"reference internal\" href=\"/pl/5.0/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>And here’s that same fixture as 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>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>You can load data by calling <a class=\"reference internal\" href=\"/pl/5.0/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=\"/pl/5.0/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=\"/pl/5.0/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=\"/pl/5.0/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\">Zobacz także</p>\n<p>Fixtures are also used by the <a class=\"reference internal\" href=\"/pl/5.0/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>","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":"Przewodniki „Jak to zrobić”","url":"/pl/5.0/howto/"}],"prev":{"docname":"howto/error-reporting","title":"Jak zarządzać zgłaszaniem błędów","url":"/pl/5.0/howto/error-reporting/"},"next":{"docname":"howto/legacy-databases","title":"How to integrate Django with a legacy database","url":"/pl/5.0/howto/legacy-databases/"},"formats":{"html":"/pl/5.0/howto/initial-data/","markdown":"/pl/5.0/howto/initial-data.md","json":"/pl/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/pl/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","1.10"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}