Quick How-To : Custom Django Management Commands
Django's manage.py runs a variety of Django admin tasks. We can add custom tasks as well. This is useful for running commands through a shell script, as with a cron job.
Say you have a package called myPackage. Let's add a custom command called doAllTheThings . This example uses Python 2.7.6 and Django 1.7 .
Here's a preview of the files we'll create.
0) Add your package to INSTALLED_APPS in
settings.py if it isn't there already.
1) In your package directory, create
management/commands subdirectories. The
__init__.py files in both to signal that these are packages. ( This may be different in Python
Forgetting to add
__init__.py will cause an error like this:
3) Create a python file with the same name as your command.
4) Here are the essential parts of a command:
5) Run the command like any other Django management command. From the top directory in your Django project,
For more detail, including how to parse arguments, see the Django developer guide