{"title":"How to provide initial data for models","version":"5.2","locale":"el","docname":"howto/initial-data","url":"/el/5.2/howto/initial-data/","canonical":"https://djangodocs.dev/el/5.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>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=\"/el/5.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=\"/el/5.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=\"/el/5.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=\"/el/5.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>Ένα fixture είναι μια συλλογή από δεδομένα την οποία το Django ξέρει πως να την κάνει import σε μια βάση δεδομένων. Ο πιο ευθύς τρόπος να δημιουργήσετε ένα fixture, αν έχετε ήδη κάποια data, είναι να χρησιμοποιήσετε την διαχειριστική εντολή <a class=\"reference internal\" href=\"/el/5.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>. Ή, μπορείτε να γράψετε τα fixtures με το χέρι. Τα fixtures μπορούν να γραφτούν σε μορφή JSON, XML ή YAML (με το <a class=\"reference external\" href=\"https://pyyaml.org/\">PyYAML</a> εγκατεστημένο, φυσικά) αρχείων. Το <a class=\"reference internal\" href=\"/el/5.2/topics/serialization/\"><span class=\"doc\">εγχειρίδιο για serialization</span></a> περιέχει περισσότερες λεπτομέρειες σχετικά με αυτού του είδους των υποστηριζόμενων <a class=\"reference internal\" href=\"/el/5.2/topics/serialization/#serialization-formats\"><span class=\"std std-ref\">μορφών serialization</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>Και εδώ φαίνεται το ίδιο fixture σε μορφή 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=\"/el/5.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=\"/el/5.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=\"/el/5.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, 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=\"/el/5.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 from 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>Τα fixtures χρησιμοποιούνται, επίσης, από το <a class=\"reference internal\" href=\"/el/5.2/topics/testing/tools/#topics-testing-fixtures\"><span class=\"std std-ref\">testing framework</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 guides","url":"/el/5.2/howto/"}],"prev":{"docname":"howto/index","title":"How-to guides","url":"/el/5.2/howto/"},"next":{"docname":"howto/legacy-databases","title":"How to integrate Django with a legacy database","url":"/el/5.2/howto/legacy-databases/"},"formats":{"html":"/el/5.2/howto/initial-data/","markdown":"/el/5.2/howto/initial-data.md","json":"/el/5.2/howto/initial-data.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/howto/initial-data.txt","official":"https://docs.djangoproject.com/el/5.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"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}