Adding Custom Commands to and

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
setup_environ( settings )

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.

  1. Damien said:


    Are there some reserved names ? Or a cache that I’d have to reload to have access to the command ?
    I’ve followed your instructions. But the command is still not found 😦

    • There are some reserved commands but “drop” is not one of them.

      Make sure that you have the file structure listed above inside one of your app directories and make sure that that app is listed inside the INSTALLED_APPS in your

      Additionally make sure you’re using the correct and that its using the right settings file. Let me know if none of that fixed it.

      • Damien said:


        I’m sorry for not replying here. But I’ve found the reason.
        Watching your article, I thought the project name was management. So I was missing the appropriate directory structure.

        Thanks 🙂

  2. Hi! first of all thanks for the info, you are so right about the lack of examples in that part, so thanks again.

    I got a question, what if my command actually requires arguments?

    example: file1

    what cass should i be using?

    thanks beforehand

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s