开发者

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

开发者 https://www.devze.com 2023-02-15 15:47 出处:网络
In Django, it\'s easy to write custom management command开发者_运维技巧s for django-admin.py/manager.py.

In Django, it's easy to write custom management command开发者_运维技巧s 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消