custom-management-commands.txt 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. .. _howto-custom-management-commands:
  2. Writing custom django-admin commands
  3. ====================================
  4. **New in Django development version**
  5. Applications can register their own actions with ``manage.py``. For example,
  6. you might want to add a ``manage.py`` action for a Django app that you're
  7. distributing.
  8. To do this, just add a ``management/commands`` directory to your application.
  9. Each Python module in that directory will be auto-discovered and registered as
  10. a command that can be executed as an action when you run ``manage.py``::
  11. blog/
  12. __init__.py
  13. models.py
  14. management/
  15. __init__.py
  16. commands/
  17. __init__.py
  18. explode.py
  19. views.py
  20. In this example, the ``explode`` command will be made available to any project
  21. that includes the ``blog`` application in ``settings.INSTALLED_APPS``.
  22. The ``explode.py`` module has only one requirement -- it must define a class
  23. called ``Command`` that extends ``django.core.management.base.BaseCommand``.
  24. For more details on how to define your own commands, look at the code for the
  25. existing ``django-admin.py`` commands, in ``/django/core/management/commands``.