payne
payne

Reputation: 14177

How to do a project-wide custom django-admin command?

In Django, it's easy to write custom management commands for django-admin.py/manager.py.

However, these commands reside at the application level, not the overall project level. You put implementations into the <project_dir>/<app_dir>/management/commands directory, and they get auto-discovered.

I have a number of commands which are project-level, not tied to any one application in the project. I can tuck them in one of the apps, but is there a way to implement project-level management commands in Django?

Upvotes: 24

Views: 3978

Answers (1)

Ski
Ski

Reputation: 14487

You can have application called core or similar, where things not tied to any application are contained. These can be management commands, temlatetags, models and maybe other modules like forms, decorators, middleware. You could use your project directory itself as "core" application.

Here is how I tend to structure my projects:

project_name
    not_reusable_app1
    not_reusable_app2
    templatetages
    tempates
    utils
    models.py
    settings.py
    management
    middleware.py
    forms.py
    processors.py
    __init__.py
parts
    reusable-app-1
        reusable_app_1
        setup.py
    reusable-app-2
    reurable-app-3
    gereric-python-lib
    django
setup.py

My INSTALLED_APPS usually looks like this:

INSTALLED_APPS = (
    ...
    'project_name',
    'project_name.not_reusable_app1',
    'reusable_app1',
    ...
)

I do not give any special treatment to django applications compred with other python packages. For example I don't put them under apps or similar directory.

It is clear that my not-reusable apps are part of project. Not reusable apps under project usually use various utilities from project, for example project_name.utils.decorators.some_kind_of_deco.

If you do not like to use project as application, like I mentioned you can move everything to project_name.core.

Upvotes: 24

Related Questions