Using fixtures to provide initial data for models

Sometimes you might want to pre-populate your database with hardcoded data. This is useful to automatically include initial data in the project setup instead of having to add it manually. Django comes with a simple way to load and dump data from the database into files that are called fixtures.

Django supports fixtures in JSON, XML, or YAML formats. We are going to create a fixture to include several initial Subject objects for our project.

First, create a superuser using the following command:

python manage.py createsuperuser

Then, run the development server using the following command:

python manage.py runserver

Open http://127.0.0.1:8000/admin/courses/subject/ in your browser. Create several subjects using the administration site. The list display page should look as follows:

Run the following command from the shell:

python manage.py dumpdata courses --indent=2

You will see output similar to the following:

[
{
"model": "courses.subject",
"pk": 1,
"fields": {
"title": "Mathematics",
"slug": "mathematics"
}
},
{
"model": "courses.subject",
"pk": 2,
"fields": {
"title": "Music",
"slug": "music"
}
},
{
"model": "courses.subject",
"pk": 3,
"fields": {
"title": "Physics",
"slug": "physics"
}
},
{
"model": "courses.subject",
"pk": 4,
"fields": {
"title": "Programming",
"slug": "programming"
}
}
]

The dumpdata command dumps data from the database into the standard output, serialized in JSON format by default. The resulting data structure includes information about the model and its fields for Django to be able to load it into the database.

You can limit the output to the models of an application by providing the application names to the command or specifying single models for outputting data using the app.Model format. You can also specify the format using the --format flag. By default, dumpdata outputs the serialized data to the standard output. However, you can indicate an output file using the --output flag. The --indent flag allows you to specify indentation. For more information on dumpdata parameters, run python manage.py dumpdata --help.

Save this dump to a fixtures file into a fixtures/ directory in the orders application using the following commands:

mkdir courses/fixtures
python manage.py dumpdata courses --indent=2 --output=courses/fixtures/subjects.json

Run the development server and use the administration site to remove the subjects you created. Then, load the fixture into the database using the following command:

python manage.py loaddata subjects.json

All Subject objects included in the fixture are loaded into the database.

By default, Django looks for files in the fixtures/ directory of each application, but you can specify the complete path to the fixture file for the loaddata command. You can also use the FIXTURE_DIRS setting to tell Django additional directories to look for fixtures.

Fixtures are not only useful for setting up initial data, but also to provide sample data for your application or data required for your tests.

You can read about how to use fixtures for testing at https://docs.djangoproject.com/en/2.0/topics/testing/tools/#fixture-loading.

If you want to load fixtures in model migrations, take a look at Django's documentation about data migrations. You can find the documentation for migrating data at https://docs.djangoproject.com/en/2.0/topics/migrations/#data-migrations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.103.183