Django Management Commands


Management commands are powerful and flexible scripts that can perform actions on your Django project or the underlying database. In addition to various default commands, it's possible to write your own!

Compared to regular Python scripts, using the management command framework means that some tedious setup work is automatically done for you behind the scenes.


Management commands can be called either from:

  • django-admin <command> [options]
  • python -m django <command> [options]
  • python <command> [options]
  • ./ <command> [options] if has execution permissions (chmod +x

To use management commands with Cron:

*/10 * * * * pythonuser /var/www/dev/env/bin/python /var/www/dev/ <command> [options] > /dev/null

Creating and Running a Management Command

To perform actions in Django using commandline or other services (where the user/request is not used), you can use the management commands.

Django modules can be imported as needed.

For each command a separate file needs to be created: myapp/management/commands/
(The management and commands directories must have an empty file)

from import BaseCommand, CommandError

# import additional classes/modules as needed
# from myapp.models import Book

class Command(BaseCommand):
    help = 'My custom django management command'

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

    def handle(self, *args, **options):
       bookid = options['book_id'] 
       author = options['author']
       # Your code goes here
        # For example:
        # books = Book.objects.filter(author="bob")
        # for book in books:
        # = "Bob"

Here class name Command is mandatory which extends BaseCommand or one of its subclasses.

The name of the management command is the name of the file containing it. To run the command in the example above, use the following in your project directory:

python my_command

Note that starting a command can take a few second (because of the import of the modules). So in some cases it is advised to create daemon processes instead of management commands.

More on management commands

Get list of existing commands

You can get list of available commands by following way:

>>> python help

If you don't understand any command or looking for optional arguments then you can use -h argument like this

>>> python command_name -h

Here command_name will be your desire command name, this will show you help text from the command.

>>> python runserver -h          
>>> usage: runserver [-h] [--version] [-v {0,1,2,3}]
                           [--settings SETTINGS] [--pythonpath PYTHONPATH]
                           [--traceback] [--no-color] [--ipv6] [--nothreading]
                           [--noreload] [--nostatic] [--insecure]

Starts a lightweight Web server for development and also serves static files.

positional arguments:
  addrport              Optional port number, or ipaddr:port

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
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g.
  --traceback           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.
  --ipv6, -6            Tells Django to use an IPv6 address.
  --nothreading         Tells Django to NOT use threading.
  --noreload            Tells Django to NOT use the auto-reloader.
  --nostatic            Tells Django to NOT automatically serve static files
                        at STATIC_URL.
  --insecure            Allows serving static files even if DEBUG is False.

List of available command

Using django-admin instead of

You can get rid of and use the django-admin command instead. To do so, you will have to manually do what does:

  • Add your project path to your PYTHONPATH
export PYTHONPATH="/home/me/path/to/your_project"
export DJANGO_SETTINGS_MODULE="your_project.settings"

This is especially useful in a virtualenv where you can set these environment variables in the postactivate script.

django-admin command has the advantage of being available wherever you are on your filesystem.

Builtin Management Commands

Django comes with a number of builtin management commands, using python [command] or, when has +x (executable) rights simply ./ [command] . The following are some of the most frequently used:

Get a list of all available commands

./ help

Run your Django server on localhost:8000; essential for local testing

./ runserver

Run a python (or ipython if installed) console with the Django settings of your project preloaded (attempting to access parts of your project in a python terminal without doing this will fail).

./ shell

Create a new database migration file based on the changes you have made to your models. See Migrations

./ makemigrations

Apply any unapplied migrations to the current database.

./ migrate

Run your project's test suite. See Unit Testing

./ test

Take all of the static files of your project and puts them in the folder specified in STATIC_ROOT so they can be served in production.

./ collectstatic

Allow to create superuser.

./ createsuperuser

Change the password of a specified user.

./ changepassword username

Full list of available commands