Adding Custom Commands to and Posted on March 13, 2009 by Dave Fowler

In Django it is very straight forward to add extra commands to the and scripts. The Django Documentation describes the process but gives no examples. I like examples so I'm writing this for others.

In the past I'd add these to make stand alone scripts that used the Django libraries.

from django.conf import settings
from import setup_environ

The method works well but I find it cleaner to add commands to the manage and django-admin scripts instead of having several stand alone scripts. The desired effect is to be able to run

$ ./ your_command_name

instead of python Big difference? No... But I think on a large project it adds organization and its some sort of a standard in the event that others will be using your code.

In the app which the command is used for make the following directory structure:


Then in your file ( paste the following code, puting the functions to run in the handle_noargs function.

from import NoArgsCommand

class Command(NoArgsCommand):
    help = "Describe the Command Here"
    def handle_noargs(self, **options):
        < your code here >

That's it! You can also do some more complicated functionality with command options. Take a look at commands listed in /django/core/management/commands for examples there.