{"title":"Come fornire i dati iniziali per i modelli","version":"5.2","locale":"it","docname":"howto/initial-data","url":"/it/5.2/howto/initial-data/","canonical":"https://djangodocs.dev/it/5.2/howto/initial-data/","summary":"A volte è utile prepopolare il tuo database con dati hard-coded al primo setup di una applicazione. Puoi fornire i dati iniziali con migrazioni o fixture. Provide…","html":"<h1>Come fornire i dati iniziali per i modelli<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>A volte è utile prepopolare il tuo database con dati hard-coded al primo setup di una applicazione. Puoi fornire i dati iniziali con migrazioni o fixture.</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=\"/it/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=\"/it/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=\"/it/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=\"/it/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>Una fixture è una collezione di dati che Django sa come importare nel database. Il modo più diretto di creare una fixture se hai già qualche dato è usare il comando <a class=\"reference internal\" href=\"/it/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>. Oppure, puoi scrivere fixture a mano; le fixture possono essere scritte come documenti JSON, XML o YAML (con <a class=\"reference external\" href=\"https://pyyaml.org/\">PyYAML</a> installato). La  <a class=\"reference internal\" href=\"/it/5.2/topics/serialization/\"><span class=\"doc\">documentazione sulla serializzazione</span></a> dà più dettagli circa ciascuno di questi <a class=\"reference internal\" href=\"/it/5.2/topics/serialization/#serialization-formats\"><span class=\"std std-ref\">serialization formats</span></a> supportati.</p>\n<p>Ad esempio, però, ecco come potrebbe apparire una fixture per un modello <code class=\"docutils literal notranslate\"><span class=\"pre\">Person</span></code> 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>Ed ecco le stesse fixtures come 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>Archivierai questi dati nella directory <code class=\"docutils literal notranslate\"><span class=\"pre\">fixtures</span></code> all’interno della tua app.</p>\n<p>Puoi caricare i dati chiamando <a class=\"reference internal\" href=\"/it/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> <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;fixturename&gt;</span></code>, dove <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;fixturename&gt;</span></code> è il nome del file di  fixture che hai creato. Ogni volta che esegui <a class=\"reference internal\" href=\"/it/5.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">loaddata</span></code></a>, i dati verranno letti dalla fixture e ricaricati nel database. Nota che questo significa che se modifichi una delle righe create da una fixture e poi esegui di nuovo <a class=\"reference internal\" href=\"/it/5.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">loaddata</span></code></a>, cancellerai qualsiasi modifica tu abbia apportato.</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=\"/it/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\">Vedi anche</p>\n<p>Le fixtures sono anche usate dal <a class=\"reference internal\" href=\"/it/5.2/topics/testing/tools/#topics-testing-fixtures\"><span class=\"std std-ref\">framework di test</span></a> come ausilio per approntare un ambiente di test consistente.</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":"/it/5.2/howto/"}],"prev":{"docname":"howto/index","title":"How-to guides","url":"/it/5.2/howto/"},"next":{"docname":"howto/legacy-databases","title":"Come integrare Django con un database legacy","url":"/it/5.2/howto/legacy-databases/"},"formats":{"html":"/it/5.2/howto/initial-data/","markdown":"/it/5.2/howto/initial-data.md","json":"/it/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/it/5.2/howto/initial-data/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}