Skip to main content

BaseCommand

The base class from which all management commands ultimately derive.

Attributes

AttributeTypeDescription
helpstring = ""A short description of the command, which will be printed in help messages.
output_transactionboolean = falseA boolean indicating whether the command outputs SQL statements; if True, the output will automatically be wrapped with BEGIN; and COMMIT;.
requires_migrations_checksboolean = falseA 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_checksstring, list, or tuple = "all"A list or tuple of tags whose registered system checks will be performed prior to executing the command, or 'all' to perform all checks.
base_stealth_optionstuple = ("stderr", "stdout")A tuple containing the standard options available to all commands that are not defined by the argument parser.
stealth_optionstuple = ()A tuple of any options the command uses which aren't defined by the argument parser.
suppressed_base_argumentsset = set()A set of default command-line arguments that should have their help text suppressed in the command's help output.

Constructor

Signature

def BaseCommand(
stdout: file-like object = None,
stderr: file-like object = None,
no_color: boolean = False,
force_color: boolean = False
) - > null

Parameters

NameTypeDescription
stdoutfile-like object = NoneThe standard output stream to use.
stderrfile-like object = NoneThe standard error stream to use.
no_colorboolean = FalseIf True, disables colorized output.
force_colorboolean = FalseIf True, forces colorized output regardless of environment.

Methods


get_version()

@classmethod
def get_version() - > string

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.

Returns

TypeDescription
stringThe current Django version string or a custom version string defined by the subclass

create_parser()

@classmethod
def create_parser(
prog_name: string,
subcommand: string
) - > [CommandParser](commandparser.md?sid=django_core_management_base_commandparser)

Create and return the ArgumentParser which will be used to parse the arguments to this command.

Parameters

NameTypeDescription
prog_namestringThe name of the program being executed from the command line
subcommandstringThe name of the specific subcommand being invoked

Returns

TypeDescription
[CommandParser](commandparser.md?sid=django_core_management_base_commandparser)An ArgumentParser instance configured with base Django options and command-specific arguments

add_arguments()

@classmethod
def add_arguments(
parser: [CommandParser](commandparser.md?sid=django_core_management_base_commandparser)
)

Entry point for subclassed commands to add custom arguments.

Parameters

NameTypeDescription
parser[CommandParser](commandparser.md?sid=django_core_management_base_commandparser)The argument parser instance to which custom command-line arguments should be added

add_base_argument()

@classmethod
def add_base_argument(
parser: [CommandParser](commandparser.md?sid=django_core_management_base_commandparser),
*args: any,
**kwargs: any
)

Call the parser's add_argument() method, suppressing the help text according to BaseCommand.suppressed_base_arguments.

Parameters

NameTypeDescription
parser[CommandParser](commandparser.md?sid=django_core_management_base_commandparser)The argument parser instance to modify
*argsanyPositional arguments passed directly to the parser's add_argument method
**kwargsanyKeyword arguments passed to the parser's add_argument method, such as help, action, or default

@classmethod
def print_help(
prog_name: string,
subcommand: string
)

Print the help message for this command, derived from self.usage().

Parameters

NameTypeDescription
prog_namestringThe name of the program to display in the help usage message
subcommandstringThe name of the subcommand to display in the help usage message

run_from_argv()

@classmethod
def run_from_argv(
argv: list
)

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.

Parameters

NameTypeDescription
argvlistA list of command-line arguments, typically sys.argv, where the first two elements are the program name and subcommand

execute()

@classmethod
def execute(
*args: any,
**options: any
) - > string

Try to execute this command, performing system checks if needed (as controlled by the requires_system_checks attribute, except if force-skipped).

Parameters

NameTypeDescription
*argsanyPositional arguments parsed from the command line to be passed to the handle method
**optionsanyNamed options and flags parsed from the command line that configure command execution

Returns

TypeDescription
stringThe output produced by the handle method, potentially wrapped in transaction SQL

get_check_kwargs()

@classmethod
def get_check_kwargs(
options: dict
) - > dict

Builds the keyword arguments used for the system check framework based on the command's requirements.

Parameters

NameTypeDescription
optionsdictThe parsed command-line options used to determine check behavior

Returns

TypeDescription
dictA dictionary containing check configuration, such as specific tags to validate

check()

@classmethod
def check(
app_configs: list,
tags: list,
display_num_errors: boolean,
include_deployment_checks: boolean,
fail_level: integer,
databases: list
)

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.

Parameters

NameTypeDescription
app_configslistA list of application configurations to restrict the check to specific apps
tagslistA list of strings representing specific check categories to run
display_num_errorsbooleanWhether to include a summary count of identified issues in the output
include_deployment_checksbooleanWhether to include checks specifically intended for deployment environments
fail_levelintegerThe severity level at which identified issues will trigger a CommandError
databaseslistA list of database aliases to run database-specific checks against

check_migrations()

@classmethod
def check_migrations()

Print a warning if the set of migrations on disk don't match the migrations in the database.


handle()

@classmethod
def handle(
*args: any,
**options: any
) - > string

The actual logic of the command. Subclasses must implement this method.

Parameters

NameTypeDescription
*argsanyPositional arguments passed to the command
**optionsanyParsed command-line options and flags

Returns

TypeDescription
stringThe output of the command, which may be printed to stdout or wrapped in a transaction