To create a custom command in Django, you first need to create an app. Click here to see how to create an app.
After you create an app inside your project, create a management
folder inside the app directory and commands
folder inside the management
folder. Inside the commands folder create a python file with the name, you want to give to your command. For example, if you want to create a command named my_command
, then the name of the python file will be my_command.py
You should edit the my_command.py
file as follows to create a command:
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'A Custom Management command'
def handle(self, *args, **kwargs):
self.stdout.write(self.style.SUCCESS('Custom command executed successfully'))
-
BaseCommand
is a base class provided by Django for creating custom management commands. - The
help
attribute is a short description of what the custom command does. It provides information that can be displayed to users when they runpython manage.py help <command_name>
to get help on the command. - The
handle
method is the entry point for your custom command's logic. When you run the custom command, Django will invoke this method. It accepts two arguments,*args
and**kwargs
. - Inside the
handle
method, theself.stdout.write
method is used to display output to the console.
You can run your command as follows :
python manage.py my_command
Users can know about your command using --help
:
python manage.py my_command --help
The following command would give an output like this :
usage: manage.py my_command [-h] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH]
[--traceback] [--no-color] [--force-color] [--skip-checks]
A Custom Management command
optional arguments:
-h, --help show this help message and exit
--version Show program's version number and exit.
-v {0,1,2,3}, --verbosity {0,1,2,3}
Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output
--settings SETTINGS The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided,
the DJANGO_SETTINGS_MODULE environment variable will be used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
--traceback Raise on CommandError exceptions.
--no-color Don't colorize the command output.
--force-color Force colorization of the command output.
--skip-checks Skip system checks.
Also, you can pass arguments in your command as follows :
class Command(BaseCommand):
help = 'A Custom Management command'
def add_arguments(self, parser):
parser.add_argument('--my_option', type=int, help='An example option')
def handle(self, *args, **kwargs):
my_option_value = kwargs['my_option']
self.stdout.write(self.style.SUCCESS(f'Custom command executed with option: {my_option_value}'))
This code accepts an argument named my_option
of type int
.
Now the argument can be passed while executing the command as follows :
python manage.py my_command --my_option 10
Now if you give help
, you will be able to see the my_option
in the arguments as well.
usage: manage.py my_command [-h] [--my_option MY_OPTION] [--version] [-v {0,1,2,3}] [--settings SETTINGS]
[--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color] [--skip-checks]
A Custom Management command
optional arguments:
-h, --help show this help message and exit
--my_option MY_OPTION
An example option
--version Show program's version number and exit.
-v {0,1,2,3}, --verbosity {0,1,2,3}
Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output
--settings SETTINGS The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided,
the DJANGO_SETTINGS_MODULE environment variable will be used.
--pythonpath PYTHONPATH
A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
--traceback Raise on CommandError exceptions.
--no-color Don't colorize the command output.
--force-color Force colorization of the command output.
--skip-checks Skip system checks.