class documentation

class BaseCommand:

Known subclasses: django.contrib.auth.management.commands.changepassword.Command, django.contrib.auth.management.commands.createsuperuser.Command, django.contrib.gis.management.commands.ogrinspect.Command, django.contrib.sessions.management.commands.clearsessions.Command, django.contrib.sitemaps.management.commands.ping_google.Command, django.contrib.staticfiles.management.commands.collectstatic.Command, django.core.management.base.AppCommand, django.core.management.base.LabelCommand, django.core.management.commands.check.Command, django.core.management.commands.compilemessages.Command, django.core.management.commands.createcachetable.Command, django.core.management.commands.dbshell.Command, django.core.management.commands.diffsettings.Command, django.core.management.commands.dumpdata.Command, django.core.management.commands.flush.Command, django.core.management.commands.inspectdb.Command, django.core.management.commands.loaddata.Command, django.core.management.commands.makemessages.Command, django.core.management.commands.makemigrations.Command, django.core.management.commands.migrate.Command, django.core.management.commands.runserver.Command, django.core.management.commands.sendtestemail.Command, django.core.management.commands.showmigrations.Command, django.core.management.commands.sqlflush.Command, django.core.management.commands.sqlmigrate.Command, django.core.management.commands.squashmigrations.Command, django.core.management.commands.test.Command, django.core.management.commands.testserver.Command, django.core.management.templates.TemplateCommand

View In Hierarchy

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 the subclasses defined in this file.

If you are interested in overriding/customizing various aspects of the command-parsing and -execution behavior, the normal flow works as follows:

  1. django-admin or manage.py loads the command class and calls its run_from_argv() method.
  2. The run_from_argv() method calls create_parser() to get an ArgumentParser for the arguments, parses them, performs any environment changes requested by options like pythonpath, and then calls the execute() method, passing the parsed arguments.
  3. The execute() method attempts to carry out the command by calling the handle() method with the parsed arguments; any output produced by handle() will be printed to standard output and, if the command is intended to produce a block of SQL statements, will be wrapped in BEGIN and COMMIT.
  4. If handle() or execute() raised any exception (e.g. CommandError), run_from_argv() will instead print an error message to stderr.

Thus, the handle() method is typically the starting point for subclasses; many built-in commands and command types either place all of their logic in handle(), or perform some additional parsing work in handle() and then delegate from it to more specialized methods as needed.

Several attributes affect behavior at various steps along the way:

help
A short description of the command, which will be printed in help messages.
output_transaction
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.
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.
requires_system_checks

A list or tuple of tags, e.g. [Tags.staticfiles, Tags.models]. System checks registered in the chosen tags 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__'.

To validate an individual application's models rather than all applications' models, call self.check(app_configs) from handle(), where app_configs is the list of application's configuration provided by the app registry.

stealth_options
A tuple of any options the command uses which aren't defined by the argument parser.
Method __init__ Undocumented
Method add​_arguments Entry point for subclassed commands to add custom arguments.
Method add​_base​_argument Call the parser's add_argument() method, suppressing the help text according to BaseCommand.suppressed_base_arguments.
Method check No summary
Method check​_migrations Print a warning if the set of migrations on disk don't match the migrations in the database.
Method create​_parser Create and return the ArgumentParser which will be used to parse the arguments to this command.
Method execute Try to execute this command, performing system checks if needed (as controlled by the requires_system_checks attribute, except if force-skipped).
Method get​_version Return 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.
Method handle The actual logic of the command. Subclasses must implement this method.
Method print​_help Print the help message for this command, derived from self.usage().
Method run​_from​_argv No summary
Class Variable base​_stealth​_options Undocumented
Class Variable help Undocumented
Class Variable output​_transaction Undocumented
Class Variable requires​_migrations​_checks Undocumented
Class Variable stealth​_options Undocumented
Class Variable suppressed​_base​_arguments Undocumented
Instance Variable ​_called​_from​_command​_line Undocumented
Instance Variable requires​_system​_checks Undocumented
Instance Variable stderr Undocumented
Instance Variable stdout Undocumented
Instance Variable style Undocumented
def __init__(self, stdout=None, stderr=None, no_color=False, force_color=False):
def add_arguments(self, parser):
overridden in django.contrib.auth.management.commands.changepassword.Command, django.contrib.auth.management.commands.createsuperuser.Command, django.contrib.gis.management.commands.ogrinspect.Command, django.contrib.sitemaps.management.commands.ping_google.Command, django.contrib.staticfiles.management.commands.collectstatic.Command, django.core.management.base.AppCommand, django.core.management.base.LabelCommand, django.core.management.commands.check.Command, django.core.management.commands.compilemessages.Command, django.core.management.commands.createcachetable.Command, django.core.management.commands.dbshell.Command, django.core.management.commands.diffsettings.Command, django.core.management.commands.dumpdata.Command, django.core.management.commands.flush.Command, django.core.management.commands.inspectdb.Command, django.core.management.commands.loaddata.Command, django.core.management.commands.makemessages.Command, django.core.management.commands.makemigrations.Command, django.core.management.commands.migrate.Command, django.core.management.commands.runserver.Command, django.core.management.commands.sendtestemail.Command, django.core.management.commands.showmigrations.Command, django.core.management.commands.sqlflush.Command, django.core.management.commands.sqlmigrate.Command, django.core.management.commands.squashmigrations.Command, django.core.management.commands.test.Command, django.core.management.commands.testserver.Command, django.core.management.templates.TemplateCommand
Entry point for subclassed commands to add custom arguments.
def add_base_argument(self, parser, *args, **kwargs):
Call the parser's add_argument() method, suppressing the help text according to BaseCommand.suppressed_base_arguments.
def check(self, app_configs=None, tags=None, display_num_errors=False, include_deployment_checks=False, fail_level=checks.ERROR, databases=None):
Use the system check framework to validate entire Django project. Raise CommandError for any serious message (error or critical errors). If there are only light messages (like warnings), print them to stderr and don't raise an exception.
def check_migrations(self):
Print a warning if the set of migrations on disk don't match the migrations in the database.
def create_parser(self, prog_name, subcommand, **kwargs):
Create and return the ArgumentParser which will be used to parse the arguments to this command.
def execute(self, *args, **options):
Try to execute this command, performing system checks if needed (as controlled by the requires_system_checks attribute, except if force-skipped).
def get_version(self):
Return 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.
def handle(self, *args, **options):
overridden in django.contrib.auth.management.commands.changepassword.Command, django.contrib.auth.management.commands.createsuperuser.Command, django.contrib.gis.management.commands.ogrinspect.Command, django.contrib.sessions.management.commands.clearsessions.Command, django.contrib.sitemaps.management.commands.ping_google.Command, django.contrib.staticfiles.management.commands.collectstatic.Command, django.core.management.base.AppCommand, django.core.management.base.LabelCommand, django.core.management.commands.check.Command, django.core.management.commands.compilemessages.Command, django.core.management.commands.createcachetable.Command, django.core.management.commands.dbshell.Command, django.core.management.commands.diffsettings.Command, django.core.management.commands.dumpdata.Command, django.core.management.commands.flush.Command, django.core.management.commands.inspectdb.Command, django.core.management.commands.loaddata.Command, django.core.management.commands.makemessages.Command, django.core.management.commands.makemigrations.Command, django.core.management.commands.migrate.Command, django.core.management.commands.runserver.Command, django.core.management.commands.sendtestemail.Command, django.core.management.commands.showmigrations.Command, django.core.management.commands.sqlflush.Command, django.core.management.commands.sqlmigrate.Command, django.core.management.commands.squashmigrations.Command, django.core.management.commands.test.Command, django.core.management.commands.testserver.Command, django.core.management.templates.TemplateCommand
The actual logic of the command. Subclasses must implement this method.
def print_help(self, prog_name, subcommand):
Print the help message for this command, derived from self.usage().
def run_from_argv(self, argv):
Set up any environment changes requested (e.g., Python path and Django settings), then run this command. If the command raises a CommandError, intercept it and print it sensibly to stderr. If the --traceback option is present or the raised Exception is not CommandError, raise it.
base_stealth_options: tuple[str, ...] =

Undocumented

help: str =
overridden in django.contrib.auth.management.commands.changepassword.Command, django.contrib.auth.management.commands.createsuperuser.Command, django.contrib.gis.management.commands.ogrinspect.Command, django.contrib.sessions.management.commands.clearsessions.Command, django.contrib.sitemaps.management.commands.ping_google.Command, django.contrib.staticfiles.management.commands.collectstatic.Command, django.contrib.staticfiles.management.commands.findstatic.Command, django.core.management.commands.check.Command, django.core.management.commands.compilemessages.Command, django.core.management.commands.createcachetable.Command, django.core.management.commands.dbshell.Command, django.core.management.commands.diffsettings.Command, django.core.management.commands.dumpdata.Command, django.core.management.commands.flush.Command, django.core.management.commands.inspectdb.Command, django.core.management.commands.loaddata.Command, django.core.management.commands.makemessages.Command, django.core.management.commands.makemigrations.Command, django.core.management.commands.migrate.Command, django.core.management.commands.runserver.Command, django.core.management.commands.sendtestemail.Command, django.core.management.commands.showmigrations.Command, django.core.management.commands.sqlflush.Command, django.core.management.commands.sqlmigrate.Command, django.core.management.commands.sqlsequencereset.Command, django.core.management.commands.squashmigrations.Command, django.core.management.commands.startapp.Command, django.core.management.commands.startproject.Command, django.core.management.commands.test.Command, django.core.management.commands.testserver.Command

Undocumented

suppressed_base_arguments: set =
_called_from_command_line: bool =

Undocumented

stderr =

Undocumented

stdout =

Undocumented