Django comes with a variety of command line utilities that can be either invoked using django-admin.py or the
convenient manage.py script. A nice thing about it is that you can also add your own commands. Those management
commands can be very handy when you need to interact with your application via command line using a terminal and it can
also serve as an interface to execute cron jobs. In this tutorial you are going to learn how to code your own commands.
Just before we get started, let’s take a moment to familiarize with Django’s command line interface. You are probably
already familiar with commands like startproject, runserver or collectstatic. To see a complete list of commands
you can run the command below:
Output:
We can create our own commands for our apps and include them in the list by creating a management/commands
directory inside an app directory, like below:
The name of the command file will be used to invoke using the command line utility. For example, if our command was
called my_custom_command.py, then we will be able to execute it via:
Let’s explore next our first example.
Basic Example
Below, a basic example of what the custom command should look like:
management/commands/what_time_is_it.py
Basically a Django management command is composed by a class named Command which inherits from BaseCommand. The
command code should be defined inside the handle() method.
See how we named our module what_time_is_it.py. This command can be executed as:
Output:
You may be asking yourself, how is that different from a regular Python script, or what’s the benefit of it. Well, the
main advantage is that all Django machinery is loaded and ready to be used. That means you can import models, execute
queries to the database using Django’s ORM and interact with all your project’s resources.
Handling Arguments
Django make use of the argparse, which is part of Python’s standard
library. To handle arguments in our custom command we should define a method named add_arguments.
Positional Arguments
The next example is a command that create random user instances. It takes a mandatory argument named total, which
will define the number of users that will be created by the command.
management/commands/create_users.py
Here is how one would use it:
Optional Arguments
The optional (and named) arguments can be passed in any order. In the example below you will find the definition of
an argument named “prefix”, which will be used to compose the username field:
management/commands/create_users.py
Usage:
or
If the prefix is used, the username field will be created as custom_user_oYwoxtt4vNHR. If not prefix, it will be
created simply as oYwoxtt4vNHR – a random string.
Flag Arguments
Another type of optional arguments are flags, which are used to handle boolean values. Let’s say we want to add an
--admin flag, to instruct our command to create a super user or to create a regular user if the flag is not present.
management/commands/create_users.py
Usage:
Or
Arbitrary List of Arguments
Let’s create a new command now named delete_users. In this new command we will be able to pass a list of user ids
and the command should delete those users from the database.
management/commands/delete_users.py
Usage:
Output:
We can also pass a number of ids separated by spaces, so the command will delete the users in a single call:
Output:
Styling
We could improve the previous example a little big by setting an appropriate color to the output message:
management/commands/delete_users.py
Usage is the same as before, difference now is just the output:
Output:
Below a list of all available styles, in form of a management command:
Cron Job
If you have a task that must run periodically, like generating a report every Monday. Or let’s say you have a Web
scrapper that collects data from some Website every 10 minutes. You can define this code as a management command and
simply add it to your server’s crontab like this:
The example above will execute the my_custom_command every day at 4 a.m.