2021-07-13 09:25:37 +02:00
|
|
|
|
==============================================
|
|
|
|
|
How to create custom ``django-admin`` commands
|
|
|
|
|
==============================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
2012-12-29 16:35:12 +01:00
|
|
|
|
.. module:: django.core.management
|
|
|
|
|
|
2008-08-24 00:25:40 +02:00
|
|
|
|
Applications can register their own actions with ``manage.py``. For example,
|
|
|
|
|
you might want to add a ``manage.py`` action for a Django app that you're
|
2010-06-05 07:26:04 +02:00
|
|
|
|
distributing. In this document, we will be building a custom ``closepoll``
|
2010-05-08 23:38:00 +02:00
|
|
|
|
command for the ``polls`` application from the
|
2010-08-19 21:27:44 +02:00
|
|
|
|
:doc:`tutorial</intro/tutorial01>`.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
2019-06-17 16:54:55 +02:00
|
|
|
|
To do this, add a ``management/commands`` directory to the application. Django
|
|
|
|
|
will register a ``manage.py`` command for each Python module in that directory
|
|
|
|
|
whose name doesn't begin with an underscore. For example:
|
2023-02-09 16:48:46 +01:00
|
|
|
|
|
2019-06-17 16:54:55 +02:00
|
|
|
|
.. code-block:: text
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
polls/
|
2008-08-24 00:25:40 +02:00
|
|
|
|
__init__.py
|
|
|
|
|
models.py
|
|
|
|
|
management/
|
2020-06-01 10:23:29 +02:00
|
|
|
|
__init__.py
|
2008-08-24 00:25:40 +02:00
|
|
|
|
commands/
|
2020-06-01 10:23:29 +02:00
|
|
|
|
__init__.py
|
2012-02-04 19:27:14 +01:00
|
|
|
|
_private.py
|
2010-05-08 23:38:00 +02:00
|
|
|
|
closepoll.py
|
|
|
|
|
tests.py
|
2008-08-24 00:25:40 +02:00
|
|
|
|
views.py
|
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
In this example, the ``closepoll`` command will be made available to any project
|
|
|
|
|
that includes the ``polls`` application in :setting:`INSTALLED_APPS`.
|
|
|
|
|
|
2012-02-04 19:27:14 +01:00
|
|
|
|
The ``_private.py`` module will not be available as a management command.
|
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
The ``closepoll.py`` module has only one requirement -- it must define a class
|
|
|
|
|
``Command`` that extends :class:`BaseCommand` or one of its
|
|
|
|
|
:ref:`subclasses<ref-basecommand-subclasses>`.
|
|
|
|
|
|
|
|
|
|
.. admonition:: Standalone scripts
|
|
|
|
|
|
2015-01-17 19:38:01 +01:00
|
|
|
|
Custom management commands are especially useful for running standalone
|
|
|
|
|
scripts or for scripts that are periodically executed from the UNIX crontab
|
|
|
|
|
or from Windows scheduled tasks control panel.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
|
|
|
|
To implement the command, edit ``polls/management/commands/closepoll.py`` to
|
2014-08-18 16:30:44 +02:00
|
|
|
|
look like this::
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
2016-04-26 11:56:44 +02:00
|
|
|
|
from polls.models import Question as Poll
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2023-02-28 20:53:28 +01:00
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
class Command(BaseCommand):
|
|
|
|
|
help = "Closes the specified poll for voting"
|
|
|
|
|
|
2014-06-06 17:55:56 +02:00
|
|
|
|
def add_arguments(self, parser):
|
2019-05-10 14:31:49 +02:00
|
|
|
|
parser.add_argument("poll_ids", nargs="+", type=int)
|
2014-06-06 17:55:56 +02:00
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
def handle(self, *args, **options):
|
2019-05-10 14:31:49 +02:00
|
|
|
|
for poll_id in options["poll_ids"]:
|
2010-05-08 23:38:00 +02:00
|
|
|
|
try:
|
2014-06-06 17:55:56 +02:00
|
|
|
|
poll = Poll.objects.get(pk=poll_id)
|
2010-05-08 23:38:00 +02:00
|
|
|
|
except Poll.DoesNotExist:
|
|
|
|
|
raise CommandError('Poll "%s" does not exist' % poll_id)
|
|
|
|
|
|
|
|
|
|
poll.opened = False
|
|
|
|
|
poll.save()
|
|
|
|
|
|
2015-11-08 10:47:21 +01:00
|
|
|
|
self.stdout.write(
|
|
|
|
|
self.style.SUCCESS('Successfully closed poll "%s"' % poll_id)
|
2023-02-28 20:53:28 +01:00
|
|
|
|
)
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2013-02-09 10:17:26 +01:00
|
|
|
|
.. _management-commands-output:
|
|
|
|
|
|
2010-06-05 07:26:04 +02:00
|
|
|
|
.. note::
|
|
|
|
|
When you are using management commands and wish to provide console
|
|
|
|
|
output, you should write to ``self.stdout`` and ``self.stderr``,
|
|
|
|
|
instead of printing to ``stdout`` and ``stderr`` directly. By
|
|
|
|
|
using these proxies, it becomes much easier to test your custom
|
2013-02-09 10:17:26 +01:00
|
|
|
|
command. Note also that you don't need to end messages with a newline
|
|
|
|
|
character, it will be added automatically, unless you specify the ``ending``
|
|
|
|
|
parameter::
|
|
|
|
|
|
|
|
|
|
self.stdout.write("Unterminated line", ending="")
|
2010-06-05 07:26:04 +02:00
|
|
|
|
|
|
|
|
|
The new custom command can be called using ``python manage.py closepoll
|
2019-05-10 14:31:49 +02:00
|
|
|
|
<poll_ids>``.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2014-06-06 17:55:56 +02:00
|
|
|
|
The ``handle()`` method takes one or more ``poll_ids`` and sets ``poll.opened``
|
2011-05-13 06:33:42 +02:00
|
|
|
|
to ``False`` for each one. If the user referenced any nonexistent polls, a
|
2016-01-12 18:28:55 +01:00
|
|
|
|
:exc:`CommandError` is raised. The ``poll.opened`` attribute does not exist in
|
2019-06-18 20:38:21 +02:00
|
|
|
|
the :doc:`tutorial</intro/tutorial02>` and was added to
|
2016-04-26 11:56:44 +02:00
|
|
|
|
``polls.models.Question`` for this example.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2014-06-06 17:55:56 +02:00
|
|
|
|
.. _custom-commands-options:
|
|
|
|
|
|
|
|
|
|
Accepting optional arguments
|
|
|
|
|
============================
|
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
The same ``closepoll`` could be easily modified to delete a given poll instead
|
2014-06-06 17:55:56 +02:00
|
|
|
|
of closing it by accepting additional command line options. These custom
|
2014-08-18 16:30:44 +02:00
|
|
|
|
options can be added in the :meth:`~BaseCommand.add_arguments` method like this::
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2014-06-06 17:55:56 +02:00
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
|
# Positional arguments
|
2019-05-10 14:31:49 +02:00
|
|
|
|
parser.add_argument("poll_ids", nargs="+", type=int)
|
2014-06-06 17:55:56 +02:00
|
|
|
|
|
|
|
|
|
# Named (optional) arguments
|
2016-06-02 21:56:13 +02:00
|
|
|
|
parser.add_argument(
|
|
|
|
|
"--delete",
|
2010-05-08 23:38:00 +02:00
|
|
|
|
action="store_true",
|
2016-06-02 21:56:13 +02:00
|
|
|
|
help="Delete poll instead of closing it",
|
|
|
|
|
)
|
2012-04-23 20:05:12 +02:00
|
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
|
# ...
|
|
|
|
|
if options["delete"]:
|
|
|
|
|
poll.delete()
|
|
|
|
|
# ...
|
|
|
|
|
|
|
|
|
|
The option (``delete`` in our example) is available in the options dict
|
2014-06-06 17:55:56 +02:00
|
|
|
|
parameter of the handle method. See the :py:mod:`argparse` Python documentation
|
|
|
|
|
for more about ``add_argument`` usage.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2010-06-05 07:26:04 +02:00
|
|
|
|
In addition to being able to add custom command line options, all
|
2016-01-12 02:59:34 +01:00
|
|
|
|
:doc:`management commands</ref/django-admin>` can accept some default options
|
|
|
|
|
such as :option:`--verbosity` and :option:`--traceback`.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2013-02-04 00:53:48 +01:00
|
|
|
|
.. _management-commands-and-locales:
|
2011-01-03 15:27:28 +01:00
|
|
|
|
|
2013-02-04 00:53:48 +01:00
|
|
|
|
Management commands and locales
|
|
|
|
|
===============================
|
2011-01-03 15:27:28 +01:00
|
|
|
|
|
2013-02-04 14:35:52 +01:00
|
|
|
|
By default, management commands are executed with the current active locale.
|
2011-01-03 15:27:28 +01:00
|
|
|
|
|
2013-02-04 14:35:52 +01:00
|
|
|
|
If, for some reason, your custom management command must run without an active
|
|
|
|
|
locale (for example, to prevent translated content from being inserted into
|
|
|
|
|
the database), deactivate translations using the ``@no_translations``
|
|
|
|
|
decorator on your :meth:`~BaseCommand.handle` method::
|
2011-01-03 15:27:28 +01:00
|
|
|
|
|
2013-02-04 14:35:52 +01:00
|
|
|
|
from django.core.management.base import BaseCommand, no_translations
|
2011-01-03 15:27:28 +01:00
|
|
|
|
|
2023-02-28 20:53:28 +01:00
|
|
|
|
|
2013-02-04 00:53:48 +01:00
|
|
|
|
class Command(BaseCommand):
|
|
|
|
|
...
|
2011-01-03 15:27:28 +01:00
|
|
|
|
|
2013-02-04 14:35:52 +01:00
|
|
|
|
@no_translations
|
2013-02-04 00:53:48 +01:00
|
|
|
|
def handle(self, *args, **options): ...
|
2011-01-03 15:27:28 +01:00
|
|
|
|
|
2013-02-04 14:35:52 +01:00
|
|
|
|
Since translation deactivation requires access to configured settings, the
|
|
|
|
|
decorator can't be used for commands that work without configured settings.
|
2011-01-03 15:27:28 +01:00
|
|
|
|
|
2014-11-15 12:13:05 +01:00
|
|
|
|
Testing
|
|
|
|
|
=======
|
|
|
|
|
|
|
|
|
|
Information on how to test custom management commands can be found in the
|
|
|
|
|
:ref:`testing docs <topics-testing-management-commands>`.
|
|
|
|
|
|
2017-08-18 22:52:06 +02:00
|
|
|
|
Overriding commands
|
|
|
|
|
===================
|
|
|
|
|
|
|
|
|
|
Django registers the built-in commands and then searches for commands in
|
|
|
|
|
:setting:`INSTALLED_APPS` in reverse. During the search, if a command name
|
|
|
|
|
duplicates an already registered command, the newly discovered command
|
|
|
|
|
overrides the first.
|
|
|
|
|
|
|
|
|
|
In other words, to override a command, the new command must have the same name
|
|
|
|
|
and its app must be before the overridden command's app in
|
|
|
|
|
:setting:`INSTALLED_APPS`.
|
|
|
|
|
|
|
|
|
|
Management commands from third-party apps that have been unintentionally
|
|
|
|
|
overridden can be made available under a new name by creating a new command in
|
|
|
|
|
one of your project's apps (ordered before the third-party app in
|
|
|
|
|
:setting:`INSTALLED_APPS`) which imports the ``Command`` of the overridden
|
|
|
|
|
command.
|
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
Command objects
|
|
|
|
|
===============
|
|
|
|
|
|
|
|
|
|
.. class:: BaseCommand
|
|
|
|
|
|
|
|
|
|
The base class from which all management commands ultimately derive.
|
|
|
|
|
|
|
|
|
|
Use this class if you want access to all of the mechanisms which
|
|
|
|
|
parse the command-line arguments and work out what code to call in
|
|
|
|
|
response; if you don't need to change any of that behavior,
|
|
|
|
|
consider using one of its :ref:`subclasses<ref-basecommand-subclasses>`.
|
|
|
|
|
|
|
|
|
|
Subclassing the :class:`BaseCommand` class requires that you implement the
|
|
|
|
|
:meth:`~BaseCommand.handle` method.
|
|
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
|
----------
|
|
|
|
|
|
2010-06-05 07:26:04 +02:00
|
|
|
|
All attributes can be set in your derived class and can be used in
|
2013-08-05 18:23:26 +02:00
|
|
|
|
:class:`BaseCommand`’s :ref:`subclasses<ref-basecommand-subclasses>`.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
|
|
|
|
.. attribute:: BaseCommand.help
|
|
|
|
|
|
2015-01-17 19:38:01 +01:00
|
|
|
|
A short description of the command, which will be printed in the
|
|
|
|
|
help message when the user runs the command
|
|
|
|
|
``python manage.py help <command>``.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2014-06-06 17:55:56 +02:00
|
|
|
|
.. attribute:: BaseCommand.missing_args_message
|
|
|
|
|
|
2015-01-17 19:38:01 +01:00
|
|
|
|
If your command defines mandatory positional arguments, you can customize
|
|
|
|
|
the message error returned in the case of missing arguments. The default is
|
|
|
|
|
output by :py:mod:`argparse` ("too few arguments").
|
2014-06-06 17:55:56 +02:00
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
.. attribute:: BaseCommand.output_transaction
|
|
|
|
|
|
2015-01-17 19:38:01 +01:00
|
|
|
|
A boolean indicating whether the command outputs SQL statements; if
|
|
|
|
|
``True``, the output will automatically be wrapped with ``BEGIN;`` and
|
|
|
|
|
``COMMIT;``. Default value is ``False``.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2016-02-12 11:02:36 +01:00
|
|
|
|
.. attribute:: BaseCommand.requires_migrations_checks
|
|
|
|
|
|
|
|
|
|
A boolean; if ``True``, the command prints a warning if the set of
|
|
|
|
|
migrations on disk don't match the migrations in the database. A warning
|
|
|
|
|
doesn't prevent the command from executing. Default value is ``False``.
|
|
|
|
|
|
2014-01-20 03:45:21 +01:00
|
|
|
|
.. attribute:: BaseCommand.requires_system_checks
|
|
|
|
|
|
2020-05-14 00:00:41 +02:00
|
|
|
|
A list or tuple of tags, e.g. ``[Tags.staticfiles, Tags.models]``. System
|
2021-04-21 16:40:04 +02:00
|
|
|
|
checks :ref:`registered in the chosen tags <registering-labeling-checks>`
|
|
|
|
|
will be checked for errors prior to executing the command. The value
|
|
|
|
|
``'__all__'`` can be used to specify that all system checks should be
|
|
|
|
|
performed. Default value is ``'__all__'``.
|
2020-05-14 00:00:41 +02:00
|
|
|
|
|
2015-11-08 10:47:21 +01:00
|
|
|
|
.. attribute:: BaseCommand.style
|
|
|
|
|
|
|
|
|
|
An instance attribute that helps create colored output when writing to
|
|
|
|
|
``stdout`` or ``stderr``. For example::
|
|
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS("..."))
|
|
|
|
|
|
|
|
|
|
See :ref:`syntax-coloring` to learn how to modify the color palette and to
|
|
|
|
|
see the available styles (use uppercased versions of the "roles" described
|
|
|
|
|
in that section).
|
|
|
|
|
|
2016-01-12 02:59:34 +01:00
|
|
|
|
If you pass the :option:`--no-color` option when running your command, all
|
|
|
|
|
``self.style()`` calls will return the original string uncolored.
|
2015-11-08 10:47:21 +01:00
|
|
|
|
|
2021-07-25 01:16:00 +02:00
|
|
|
|
.. attribute:: BaseCommand.suppressed_base_arguments
|
|
|
|
|
|
|
|
|
|
The default command options to suppress in the help output. This should be
|
|
|
|
|
a set of option names (e.g. ``'--verbosity'``). The default values for the
|
|
|
|
|
suppressed options are still passed.
|
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
Methods
|
|
|
|
|
-------
|
|
|
|
|
|
|
|
|
|
:class:`BaseCommand` has a few methods that can be overridden but only
|
|
|
|
|
the :meth:`~BaseCommand.handle` method must be implemented.
|
|
|
|
|
|
|
|
|
|
.. admonition:: Implementing a constructor in a subclass
|
|
|
|
|
|
2014-08-18 16:30:44 +02:00
|
|
|
|
If you implement ``__init__`` in your subclass of :class:`BaseCommand`,
|
|
|
|
|
you must call :class:`BaseCommand`’s ``__init__``::
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2014-08-18 16:30:44 +02:00
|
|
|
|
class Command(BaseCommand):
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-22 07:57:14 +01:00
|
|
|
|
super().__init__(*args, **kwargs)
|
2014-08-18 16:30:44 +02:00
|
|
|
|
# ...
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2018-06-11 19:20:50 +02:00
|
|
|
|
.. method:: BaseCommand.create_parser(prog_name, subcommand, **kwargs)
|
|
|
|
|
|
|
|
|
|
Returns a ``CommandParser`` instance, which is an
|
|
|
|
|
:class:`~argparse.ArgumentParser` subclass with a few customizations for
|
|
|
|
|
Django.
|
|
|
|
|
|
|
|
|
|
You can customize the instance by overriding this method and calling
|
|
|
|
|
``super()`` with ``kwargs`` of :class:`~argparse.ArgumentParser` parameters.
|
|
|
|
|
|
2014-06-06 17:55:56 +02:00
|
|
|
|
.. method:: BaseCommand.add_arguments(parser)
|
|
|
|
|
|
|
|
|
|
Entry point to add parser arguments to handle command line arguments passed
|
|
|
|
|
to the command. Custom commands should override this method to add both
|
|
|
|
|
positional and optional arguments accepted by the command. Calling
|
|
|
|
|
``super()`` is not needed when directly subclassing ``BaseCommand``.
|
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
.. method:: BaseCommand.get_version()
|
|
|
|
|
|
2015-01-17 18:49:16 +01:00
|
|
|
|
Returns the Django version, which should be correct for all built-in Django
|
|
|
|
|
commands. User-supplied commands can override this method to return their
|
|
|
|
|
own version.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
|
|
|
|
.. method:: BaseCommand.execute(*args, **options)
|
|
|
|
|
|
2015-01-17 18:49:16 +01:00
|
|
|
|
Tries to execute this command, performing system checks if needed (as
|
|
|
|
|
controlled by the :attr:`requires_system_checks` attribute). If the command
|
2022-03-10 10:20:25 +01:00
|
|
|
|
raises a :exc:`CommandError`, it's intercepted and printed to ``stderr``.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2012-03-02 18:13:53 +01:00
|
|
|
|
.. admonition:: Calling a management command in your code
|
|
|
|
|
|
|
|
|
|
``execute()`` should not be called directly from your code to execute a
|
2016-01-13 00:01:29 +01:00
|
|
|
|
command. Use :func:`~django.core.management.call_command` instead.
|
2012-03-02 18:13:53 +01:00
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
.. method:: BaseCommand.handle(*args, **options)
|
|
|
|
|
|
|
|
|
|
The actual logic of the command. Subclasses must implement this method.
|
|
|
|
|
|
2017-01-20 22:04:05 +01:00
|
|
|
|
It may return a string which will be printed to ``stdout`` (wrapped
|
2015-10-06 21:08:15 +02:00
|
|
|
|
by ``BEGIN;`` and ``COMMIT;`` if :attr:`output_transaction` is ``True``).
|
|
|
|
|
|
2024-01-20 09:37:13 +01:00
|
|
|
|
.. method:: BaseCommand.check(app_configs=None, tags=None,display_num_errors=False, include_deployment_checks=False, fail_level=checks.ERROR, databases=None)
|
2012-12-29 16:35:12 +01:00
|
|
|
|
|
2014-01-20 03:45:21 +01:00
|
|
|
|
Uses the system check framework to inspect the entire Django project for
|
2016-01-12 18:28:55 +01:00
|
|
|
|
potential problems. Serious problems are raised as a :exc:`CommandError`;
|
2022-03-10 10:20:25 +01:00
|
|
|
|
warnings are output to ``stderr``; minor notifications are output to
|
|
|
|
|
``stdout``.
|
2014-01-20 03:45:21 +01:00
|
|
|
|
|
2014-06-26 11:33:09 +02:00
|
|
|
|
If ``app_configs`` and ``tags`` are both ``None``, all system checks are
|
2024-01-20 09:37:13 +01:00
|
|
|
|
performed except deployment and database related checks. ``tags`` can be a
|
|
|
|
|
list of check tags, like ``compatibility`` or ``models``.
|
|
|
|
|
|
|
|
|
|
You can pass ``include_deployment_checks=True`` to also perform deployment
|
|
|
|
|
checks, and list of database aliases in the ``databases`` to run database
|
|
|
|
|
related checks against them.
|
2014-01-20 03:45:21 +01:00
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
.. _ref-basecommand-subclasses:
|
|
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
|
``BaseCommand`` subclasses
|
|
|
|
|
--------------------------
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
|
|
|
|
.. class:: AppCommand
|
|
|
|
|
|
2013-12-24 23:43:47 +01:00
|
|
|
|
A management command which takes one or more installed application labels as
|
|
|
|
|
arguments, and does something with each of them.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
2013-12-24 23:43:47 +01:00
|
|
|
|
Rather than implementing :meth:`~BaseCommand.handle`, subclasses must
|
|
|
|
|
implement :meth:`~AppCommand.handle_app_config`, which will be called once for
|
|
|
|
|
each application.
|
|
|
|
|
|
|
|
|
|
.. method:: AppCommand.handle_app_config(app_config, **options)
|
|
|
|
|
|
|
|
|
|
Perform the command's actions for ``app_config``, which will be an
|
|
|
|
|
:class:`~django.apps.AppConfig` instance corresponding to an application
|
|
|
|
|
label given on the command line.
|
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
.. class:: LabelCommand
|
|
|
|
|
|
2015-01-17 19:38:01 +01:00
|
|
|
|
A management command which takes one or more arbitrary arguments (labels) on
|
|
|
|
|
the command line, and does something with each of them.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
|
|
|
|
Rather than implementing :meth:`~BaseCommand.handle`, subclasses must implement
|
|
|
|
|
:meth:`~LabelCommand.handle_label`, which will be called once for each label.
|
|
|
|
|
|
2017-01-17 15:57:26 +01:00
|
|
|
|
.. attribute:: LabelCommand.label
|
|
|
|
|
|
|
|
|
|
A string describing the arbitrary arguments passed to the command. The
|
|
|
|
|
string is used in the usage text and error messages of the command.
|
|
|
|
|
Defaults to ``'label'``.
|
|
|
|
|
|
2010-05-08 23:38:00 +02:00
|
|
|
|
.. method:: LabelCommand.handle_label(label, **options)
|
|
|
|
|
|
2015-01-17 19:38:01 +01:00
|
|
|
|
Perform the command's actions for ``label``, which will be the string as
|
|
|
|
|
given on the command line.
|
2010-05-08 23:38:00 +02:00
|
|
|
|
|
|
|
|
|
Command exceptions
|
|
|
|
|
------------------
|
|
|
|
|
|
2020-04-14 09:56:16 +02:00
|
|
|
|
.. exception:: CommandError(returncode=1)
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
2015-01-17 19:38:01 +01:00
|
|
|
|
Exception class indicating a problem while executing a management command.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
2015-01-17 19:38:01 +01:00
|
|
|
|
If this exception is raised during the execution of a management command from a
|
|
|
|
|
command line console, it will be caught and turned into a nicely-printed error
|
2022-03-10 10:20:25 +01:00
|
|
|
|
message to the appropriate output stream (i.e., ``stderr``); as a result,
|
|
|
|
|
raising this exception (with a sensible description of the error) is the
|
|
|
|
|
preferred way to indicate that something has gone wrong in the execution of a
|
|
|
|
|
command. It accepts the optional ``returncode`` argument to customize the exit
|
|
|
|
|
status for the management command to exit with, using :func:`sys.exit`.
|
2012-05-26 20:50:44 +02:00
|
|
|
|
|
2016-01-13 00:01:29 +01:00
|
|
|
|
If a management command is called from code through
|
|
|
|
|
:func:`~django.core.management.call_command`, it's up to you to catch the
|
|
|
|
|
exception when needed.
|