Django: split management commands into subfolders
Asked Answered
C

2

14

The Django management commands documentation shows all commands being created in an app/management/commands folder. Is it possible to put commands into subfolders, like app/management/commands/install and app/management/commands/maintenance? How would this be done?

Chainey answered 25/8, 2012 at 20:7 Comment(3)
can you give more details on your problem? is it what you are looking for? docs.djangoproject.com/en/dev/howto/custom-management-commandsKoblick
I'd like to put commands into subfolders of app/management/commands. I've added more detail to the question.Chainey
still not possible? after 7 years?Bender
M
7

Unfortunatly, as of Django 1.4 there seems to be no way of doing that. The sources for django.core.management.__init__.py have this method:

def find_commands(management_dir):
    """
    Given a path to a management directory, returns a list of all the command
    names that are available.

    Returns an empty list if no commands are defined.
    """
    command_dir = os.path.join(management_dir, 'commands')
    try:
        return [f[:-3] for f in os.listdir(command_dir)
                if not f.startswith('_') and f.endswith('.py')]
    except OSError:
       return []

As you can see, it only considers files directly inside the commands folder, ignoring any subfolders. However, if you "monkey patch" this function somehow, the rest of the code should work fine, since the code that actually creates the Command instance is this:

def load_command_class(app_name, name):
    """
    Given a command name and an application name, returns the Command
    class instance. All errors raised by the import process
    (ImportError, AttributeError) are allowed to propagate.
    """
    module = import_module('%s.management.commands.%s' % (app_name, name))
    return module.Command()

So, if you had a command named subfolder.command it would load the right script and instantiate the right class.

From a practical standpoint, however, I see no use of doing that. Sure, having "namespace'd" commands would be nice, but you can always prefix all your commands with some name if you want, using something else as a separator (such as _). The command name length - and the number of keystrokes needed to type them in the terminal - will be the same...

Micropaleontology answered 9/10, 2012 at 3:12 Comment(1)
"From a practical standpoint" - irrelevant; names and layouts are a subjective domain, and one shouldn't have to prove a "practical" benefit in order to implement their preference to split up a folder of 50+ files into smaller groups.Setback
C
0

Here is my example:

app
- management
  - install
      __init__.py
      check1.py
  - maintenance
      __init__.py
      check2.py
  - commands
    __init__.py
    daily_cron.py

In the daily_cron.py I used:

 from app.management.install import check1
 from app.management.maintenance import check2
 
 flag = check1(args)
 flag2 = check2(args)

Do remember to put __init__.py empty file in each new folder

Clute answered 29/6, 2021 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.