how-to commands   0   629
How to create management commands in Django

You must have used createsuperuser command in your Django application at one or other time. If not then I am sure you must have used makemigrations or migrate commands in your project. Yes? Yes.

So these commands, also called as management commands are used to execute some piece of code from command line.

In this article I will tell you how to create your own command.

Why do you need management command:

First thing that comes to your mind after reading above paragraph is that why do even I need management command in my project.

Lets say you have a project where you need to perform some task periodically.

For example: updating database column after fetching data from some other server, sending lots of mails, taking backup of your database etc etc.

How to create a management command:

Follow below steps to create your own management command.

-  Create a directory in your application and name it management .
-  Create a blank  file inside it.
-  Create a directory named as commands inside management directory.
-  Create a blank file inside commands  directory.

 |    |-commands
      |   |
      |   |

- Inside commands directory create a file with name of your command. Lets say
- Add below code to this file.

from import BaseCommand, CommandError

class Command(BaseCommand):
    help = 'Command to do........'

    def add_argument(self, parser):
    def handle(self, *args, **options):
            # your logic here
            print("I am here")
        except Exception as e:

 - We must define a class with name Command which extends BaseCommand.
 - Here add_argument function is used to add the arguments to command on command line.
- Write whatever login you want to execute in handle function. Pro tip here: If your logic is very lengthy, you can write it as a separate service and call that service function in your handle function.
 - Save the files and on terminal execute your command. You will see the output on the screen.

$ python mycommand

Positional Arguments:

You may pass and accept the command line positional arguments to your command.

def add_arguments(self, parser):
        parser.add_argument('msg', nargs='+', type=str)

Now you may use this variable msg in your command.

def handle(self, *args, **options):
            # your logic here
            msg = options["msg"]
            print("I am here")

        except Exception as e:

You can use this command in crontab to execute is periodically.

Read this article on how to schedule a task on server.  

Code on Github:

Please use this github link to download sample code for this article.
Github URL :

how-to commands   0   629

Related Articles:
Python Script 3: Validate, format and Beautify JSON string Using Python
Validating json using python code, format and beautify json file using python, validate json file using python, how to validate, format and beautify json...
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in DJango. Encrypting the critical information in Django App. Encrypting username, email and password in Django...
Adding Robots.txt file to Django Application
Adding robots.txt file in your Django application. Easiest way to add robots.txt file. Django application robots.txt file. Why should you add robots.txt file in your Django Application....
Scraping 10000 tweets in 60 seconds using celery, RabbitMQ and Docker cluster with rotating proxy
Scrapping large amount of tweets within minutes using celery, RabbitMQ and docker cluster. Scraping huge data quickly using docker cluster with TOR....

0 thoughts on 'How To Create Management Commands In Django'
Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

Please subscribe to get the latest articles in your mailbox.

Recent Posts:

© 2018-2019