Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

django

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?

like image 938
payne Avatar asked Mar 06 '11 13:03

payne


People also ask

What is Django admin commands?

Some of the most commonly used commands are – python manage.py startapp. python manage.py makemigrations. python manage.py migrate. python manage.py runserver.

What does the Django management command Inspectdb do?

inspectdb. Introspects the database tables in the database pointed-to by the NAME setting and outputs a Django model module (a models.py file) to standard output.


1 Answers

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.

like image 113
Ski Avatar answered Oct 01 '22 19:10

Ski