By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

Django Custom Management Command

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

For example:

# 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. This will make that every other python file in the commands directory can be executed via manage.py command.

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.

 

Example: Management command to create elasticsearch index.

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

 

management/

    __init__.py

    commands/

        __init__.py

        create_index.py

 

#in create_index.py file add the following code.
import json, requests
from django.core.management.base 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 = 'http://127.0.0.1:9200/my_index/'
        response = requests.put(url, data=json.dumps(data))
        print(response.text)
 
 

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 manage.py create_index

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

    Posted On
  • 21 July 2012
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How To Write Custom migrations in Django

This blog post will help you to get deeper into Django Migrations - how Migrations works and how to create or add our custom migrations. ...

Continue Reading...
Django Single Sign On(SSO) to multiple applications

Single sign on is a way for users to issue a security token for the first time login, login into multiple applications using one set ...

Continue Reading...
How to create Custom User Model or Extend User Model in Django?

Django provides built in authentication which is good for most of the cases, but you may have needs that are being served with the existing ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties