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 ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.