Django Initial test Data setup - Using custom django commands

One of the important part of any django project development includes unit testing API with initial set of test data’s.

And it doen’t make sense to add these initial data manually every time ,when it is git pulled by other developer in different system.

Instead a robust method is to load them as JSON fixtures. In this post we’ll move a step forward and create custom django command to do the same.


First let’s learn how django’s inbuilt management command dumpdata and loaddata works

DUMPDATA COMMAND

It is a django management command, which can be use to backup(export) your model instances or whole database

Python manage.py dumpdata <app_name> - -indent 2 > <app_name>.json
  • indent 2(optional) : command just gives indentation space of 2

example: python manage.py dumpdata profile > profile.json

Example output of above command is below

LOADDATA COMMAND

This command can be use to load the fixtures(database dumps) into database

before running this command make sure makemigrations and migrate is done.

Python manage.py loaddata <app_name>.json

example: python manage.py loaddata profiles.json

Note: dumpdata FIXTURE ( ie :<app_name>.json )must be actually present in FIXTURE folder (eg: profiles.json )before loaddata command can be executed.

Custom Django commands to load and dump data

STEP 1:

create a new folder named myCommands in your django project folder

create management folder in myCommand folder

and commands Folder in management folder

and create command files within them

#tree structure of files
|--djangoProject
| |--djangoApp
| |--django-Project-settings
| |--myCommands
|--management
|--commands
|--loadinitialdata.py
|--dumpinitialdata.py

STEP 2:

create a folder by name FIXTURE and a file by named db.json inside it.

#tree structure of files
|--djangoProject
| |--djangoApp
| |--django-Project-settings
| |--myCommands
|
| |--FIXTURE
| |--db.json

STEP 3:

open settings.py and add FIXTURE and myCommands in Installed APPs

#
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

#custom command apps
'myCommands',
'FIXTURE',
)

STEP 4:

create a command file named dumpinitialdata.py inside commands folder

and add following code.

#
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from django.core.management import call_command
import json
import sys
class Command(BaseCommand):
args = ''
help = 'Loads the initial data in to database'
def handle(self, *args, **options):
sysout = sys.stdout
sys.stdout = open('FIXTURE/db.json', 'w')
call_command('dumpdata')
sys.stdout = sysout
#call_command('loaddata', 'peeldb/fixtures/countries.json', verbosity=0)
result = {'message': "Successfully dumping initial data"}
return json.dumps(result)

create a custom command file named loadinitialdata.py and add commands shown below.

#
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from django.core.management import call_command
import json
import sys
class Command(BaseCommand):
args = ''
help = 'Loads the initial data in to database'
def handle(self, *args, **options):
call_command('makemigrations')
call_command('migrate')
call_command('loaddata', 'FIXTURE/db.json',app_label='FIXTURE')
#call_command('loaddata', 'peeldb/fixtures/countries.json', verbosity=0)
result = {'message': "Successfully loaded initial data"}
return json.dumps(result)

how to run custom commands ?

Run command

python manage.py dumpinitialdata

this command will dump the data from database to db.json file present in FIXTURE.

python manage.py loadinitialdata

this command will load data from FIXTURE back to database .

so next time when code is git pulled by other developer . All he need to do is just run loadinitialdata command and it will add all the data back to datebase model.

Happy Coding Folks…