How to provide initial data in Django Models?

Suyoj Man Tamrakar
3 min readApr 2, 2021

There are times when you have to prepopulate your database with some hard-coded data when you are first setting up a Django app. There are basically two ways in which you can provide initial data to your Django models.

  1. Providing initial data with migrations
  2. Providing data with fixtures
Django project with a person app

Here, we will discuss both ways in which we can populate data in our database. Firstly, Django can’t automatically generate data migrations for you, as it does with schema migrations, but it’s not very hard to write them. Migration files in Django are made up of Operations, and the main operation you use for data migrations is RunPython. For example, we have a simple app named Person and have an Author as a model.

After writing your models now create the schema by makemigrations command. Now, let’s write a migration that populates first_name to the model Author. All we need to do is use RunPython in the migrations file just created.

from django.db import migrations, modelsdef populate_names(apps, schema_editor):
names = ["Suyoj", "Sanjay", "Sangeeta"]
Author = apps.get_model('person', 'Author')
for name in names:
obj = Author(first_name=name)
obj.save()
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True,serialize=False, verbose_name='ID')),
('first_name', models.CharField(max_length=100)),
],
options={
'db_table': 'Author',
},
),
migrations.RunPython(populate_names), #Just add this line
]

Once that’s done, we can run python manage.py migrate as normal and the data migration will run in place alongside other migrations.

Secondly, you can also provide data using fixtures. A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you’ve already got some data is to use the manage.py dumpdata command. Or, you can write fixtures by hand; fixtures can be written as JSON, XML, or YAML (with PyYAML installed) documents. As an example, though, here’s what a fixture for an Author model might look like in JSON:

# (data.json)
[
{
"model": "person.author", # "model" : "app_name.model_name"
"pk": 4,
"fields": {
"first_name": "Sonika"
}
},
{
"model": "person.author",
"pk": 5,
"fields": {
"first_name": "Ashish"
}
}
]
#Command : python manage.py loaddata data.json

You can load data by calling manage.py loaddata <fixturename>, where <fixturename> is the name of the fixture file you’ve created. Each time you run loaddata, the data will be read from the fixture and re-loaded into the database.

fixtures directory inside the app directory

By default, Django looks in the fixtures directory inside each app for fixtures. You can set the FIXTURE_DIRS setting to a list of additional directories where Django should look.

In this way, we can prepopulate data in our models in Django.

--

--