---
title: "Come fornire i dati iniziali per i modelli"
version: 5.0
locale: it
source: https://docs.djangoproject.com/it/5.0/howto/initial-data/
canonical: https://djangodocs.dev/it/5.0/howto/initial-data/
---
# Come fornire i dati iniziali per i modelli

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 initial data with migrations

To automatically load initial data for an app, create a
[data migration](/it/5.0/topics/migrations/#data-migrations). Migrations are run when setting up the
test database, so the data will be available there, subject to [some
limitations](/it/5.0/topics/testing/overview/#test-case-serialized-rollback).

## Provide data with fixtures

You can also provide data using [fixtures](/it/5.0/topics/db/fixtures/#fixtures-explanation),
however, this data isn’t loaded automatically, except if you use
[`TransactionTestCase.fixtures`](/it/5.0/topics/testing/tools/#django.test.TransactionTestCase.fixtures).

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 [`manage.py dumpdata`](/it/5.0/ref/django-admin/#django-admin-dumpdata). Oppure, puoi scrivere fixture a mano; le fixture possono essere scritte come documenti JSON, XML o YAML (con [PyYAML](https://pyyaml.org/) installato). La  [documentazione sulla serializzazione](/it/5.0/topics/serialization/) dà più dettagli circa ciascuno di questi [serialization formats](/it/5.0/topics/serialization/#serialization-formats) supportati.

Ad esempio, però, ecco come potrebbe apparire una fixture per un modello `Person` in JSON:

```js
[
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Lennon"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]
```

Ed ecco le stesse fixtures come YAML:

```yaml
- model: myapp.person
  pk: 1
  fields:
    first_name: John
    last_name: Lennon
- model: myapp.person
  pk: 2
  fields:
    first_name: Paul
    last_name: McCartney
```

Archivierai questi dati nella directory `fixtures` all’interno della tua app.

Puoi caricare i dati chiamando [`manage.py loaddata`](/it/5.0/ref/django-admin/#django-admin-loaddata) `<fixturename>`, dove `<fixturename>` è il nome del file di  fixture che hai creato. Ogni volta che esegui [`loaddata`](/it/5.0/ref/django-admin/#django-admin-loaddata), 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 [`loaddata`](/it/5.0/ref/django-admin/#django-admin-loaddata), cancellerai qualsiasi modifica tu abbia apportato.

### Tell Django where to look for fixture files

By default, Django looks for fixtures in the `fixtures` directory inside each
app for, so the command `loaddata sample` will find the file
`my_app/fixtures/sample.json`. This works with relative paths as well, so
`loaddata my_app/sample` will find the file
`my_app/fixtures/my_app/sample.json`.

Django also looks for fixtures in the list of directories provided in the
[`FIXTURE_DIRS`](/it/5.0/ref/settings/#std-setting-FIXTURE_DIRS) setting.

To completely prevent default search form happening, use an absolute path to
specify the location of your fixture file, e.g. `loaddata /path/to/sample`.

> **Namespace your fixture files**
>
> Django will use the first fixture file it finds whose name matches, so if
> you have fixture files with the same name in different applications, you
> will be unable to distinguish between them in your `loaddata` commands.
> The easiest way to avoid this problem is by *namespacing* your fixture
> files. That is, by putting them inside a directory named for their
> application, as in the relative path example above.

> **See also**
>
> Le fixtures sono anche usate dal [framework di test](/it/5.0/topics/testing/tools/#topics-testing-fixtures) come ausilio per approntare un ambiente di test consistente.
