Login

Management command to list custom management commands

Author:
pbx
Posted:
June 11, 2009
Language:
Python
Version:
1.0
Tags:
management commands
Score:
5 (after 5 ratings)

I work with multiple projects, many of which have multiple custom management commands defined. It can be hard to remember them, and slow to pick them out of the "manage.py help" list.

This quickie command lists all of a project's custom commands (along with their help text). Writing it was easy after looking at the source of django.core.management.

Open questions include: how do you decide which app to put this command in? Should this command list itself?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from django.core.management import find_management_module, find_commands, load_command_class
from django.core.management.base import NoArgsCommand
from django.conf import settings


class Command(NoArgsCommand):
    help = "Show the list of custom management commands in this project."
    requires_model_validation = False

    def handle_noargs(self, **options):
        app_names = [a for a in settings.INSTALLED_APPS if not a.startswith("django.")]
        print "Custom management commands in this project:"
        for app_name in app_names:
            command_names = find_commands(find_management_module(app_name))
            for command_name in command_names:
                help_text = load_command_class(app_name, command_name).help
                print "%s\n\t%s (%s)\n" % (command_name, help_text, app_name)
        if not app_names:
            print "None"

More like this

  1. Management command which helps to find temlate files by skyjur 4 years, 3 months ago
  2. Custom Command for Rebuilding Permissions and ContentTypes by cronosa 4 years, 1 month ago
  3. Using manager methods by ubernostrum 8 years, 2 months ago
  4. extras.py for management commands by dnordberg 7 years, 8 months ago
  5. collectmedia command: Copy or link media files from installed apps by exogen 6 years, 7 months ago

Comments

andybak (on June 11, 2009):

You should suggest including this with Django Extensions. That seems like an ideal home for it.

#

jezdez (on June 11, 2009):

Very cool, I second andybak's suggestion. If you fork django-extensions on Github and send us a pull request I can add it right away. Oh, there is one thing, the find_management_module call in line 14 will raise an ImportError on 1.1.

#

Please login first before commenting.