Django Custom Management Command

Reading Time : ~ .

In Django project, We could manage by executing some commands which could be invoked through the manage.py.

For example:

# Sync database
python manage.py syncdb

# Start the webserver
python manage.py runserver

Management Command is used to create our own actions with manage.py.

If your app is in projectdirectory then create the directories projectdirectory/app/management/commands.

Create an empty file called __init__.py in both the management and commands directories. Once you’ve done that every other python file in the commands directory can be executed via manage.py.

If you create the file project/app/management/commands/management_command.py. Django will register a manage.py command for each Python module in that directory whose name doesn’t begin with an underscore, then it can be run as manage.py management_command.

In the app which the command is used for make the following directory structure:

management/
    __init__.py
    commands/
        __init__.py
        your_command_name.py


from django.core.management.base import BaseCommand, CommandError

from django.conf import settings

from django.core.management import call_command

import json

class Command(BaseCommand):

    args = ''

    help = 'Loads the initial data in to database'

    def handle(self, *args, **options):
        # Your Code
        call_command('loaddata', 'peeldb/fixtures/countries.json', verbosity=0)
        call_command('loaddata', 'peeldb/fixtures/states.json', verbosity=0)
        call_command('loaddata', 'peeldb/fixtures/cities.json', verbosity=0)
        call_command('loaddata', 'peeldb/fixtures/skills.json', verbosity=0)
        call_command('loaddata', 'peeldb/fixtures/industries.json', verbosity=0)
        call_command('loaddata', 'peeldb/fixtures/qualification.json', verbosity=0)
        result = {'message': "Successfully Loading initial data"}
        return json.dumps(result)

In the above example, I'm using management command to load the initial data from the fixtures into the database.In the above code, call_command calls the built-in management commands, with the given options and args/kwargs.

Some examples:
call_command(‘syncdb’), call_command(‘shell’, plain=True), call_command(‘sqlall’, ‘myapp’)

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Export html web page to pdf using jspdf Vidyasagar Rudraram

jsPDF is used to generate pdf files in client-side Javascript. You can find the links for jsPDF here and also you can find the link ...

Continue Reading...
How to Document API Requests using Django Rest Swagger Swetha Naretla

Django Rest Swagger is used to Document your API for eg., Listing all Your project Rest API's in the browser with brief description about the ...

Continue Reading...
Deploying Your Django app on Heroku Swetha Naretla

Heroku is a platform as a service (PaaS) that enables developers to build and run applications entirely in the cloud.
1. installation
2. Creating and ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties