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. Migrations are run when setting up the test database, so the data will be available there, subject to some limitations.

Provide data with fixtures

You can also provide data using fixtures, however, this data isn’t loaded automatically, except if you use 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. Oppure, puoi scrivere fixture a mano; le fixture possono essere scritte come documenti JSON, XML o YAML (con PyYAML installato). La documentazione sulla serializzazione dà più dettagli circa ciascuno di questi serialization formats supportati.

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

[
  {
    "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:

- 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 <fixturename>, dove <fixturename> è il nome del file di fixture che hai creato. Ogni volta che esegui 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, 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 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.

Vedi anche

Le fixtures sono anche usate dal framework di test come ausilio per approntare un ambiente di test consistente.

Back to Top