Innovate anywhere, anytime Your cloud-based dev studio.

Django Custom Management Commands


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

For example:

# Start the webserver
python runserver

Management Command is used to create our own actions with

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

Create an empty file called in both the management and commands directories. This will make that every other python file in the commands directory can be executed via command.

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

Example: Management command to create elasticsearch index.

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






#in file add the following code.

import json, requests

from import BaseCommand, CommandError

class Command(BaseCommand):

help = 'create elasticsearch index'

def handle(self, *args, **options):

    data = {

        "settings": {

            "number_of_shards": 4,

                "number_of_replicas": 1


        "mappings": {

        "notebook": {

        "properties": {

        "title": { "type": "string", "boost": 4 },

            "description": { "type": "string", "boost": 2 },

                "content": { "type": "string" }





        url = ''

        response = requests.put(url, data=json.dumps(data))


In the above example, I'm using management command to create a my_index with title, description and content. Now run the following command to create index.

python create_index

Above command should return the response '{"acknowledged":true}' after successful index creation.