The base class from which all management commands ultimately derive.
Attributes
| Attribute | Type | Description |
|---|
| help | string = "" | A short description of the command, which will be printed in help messages. |
| output_transaction | boolean = false | A boolean indicating whether the command outputs SQL statements; if True, the output will automatically be wrapped with BEGIN; and COMMIT;. |
| requires_migrations_checks | boolean = false | 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 | string, 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_options | tuple = ("stderr", "stdout") | A tuple containing the standard options available to all commands that are not defined by the argument parser. |
| stealth_options | tuple = () | A tuple of any options the command uses which aren't defined by the argument parser. |
| suppressed_base_arguments | set = 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
| Name | Type | Description |
|---|
| stdout | file-like object = None | The standard output stream to use. |
| stderr | file-like object = None | The standard error stream to use. |
| no_color | boolean = False | If True, disables colorized output. |
| force_color | boolean = False | If 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
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| prog_name | string | The name of the program being executed from the command line |
| subcommand | string | The name of the specific subcommand being invoked |
Returns
| Type | Description |
|---|
[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
| Name | Type | Description |
|---|
| 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
| Name | Type | Description |
|---|
| parser | [CommandParser](commandparser.md?sid=django_core_management_base_commandparser) | The argument parser instance to modify |
| *args | any | Positional arguments passed directly to the parser's add_argument method |
| **kwargs | any | Keyword arguments passed to the parser's add_argument method, such as help, action, or default |
print_help()
@classmethod
def print_help(
prog_name: string,
subcommand: string
)
Print the help message for this command, derived from self.usage().
Parameters
| Name | Type | Description |
|---|
| prog_name | string | The name of the program to display in the help usage message |
| subcommand | string | The 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
| Name | Type | Description |
|---|
| argv | list | A 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
| Name | Type | Description |
|---|
| *args | any | Positional arguments parsed from the command line to be passed to the handle method |
| **options | any | Named options and flags parsed from the command line that configure command execution |
Returns
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| options | dict | The parsed command-line options used to determine check behavior |
Returns
| Type | Description |
|---|
dict | A 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
| Name | Type | Description |
|---|
| app_configs | list | A list of application configurations to restrict the check to specific apps |
| tags | list | A list of strings representing specific check categories to run |
| display_num_errors | boolean | Whether to include a summary count of identified issues in the output |
| include_deployment_checks | boolean | Whether to include checks specifically intended for deployment environments |
| fail_level | integer | The severity level at which identified issues will trigger a CommandError |
| databases | list | A 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
| Name | Type | Description |
|---|
| *args | any | Positional arguments passed to the command |
| **options | any | Parsed command-line options and flags |
Returns
| Type | Description |
|---|
string | The output of the command, which may be printed to stdout or wrapped in a transaction |