Skip to main content

Command

This class provides a command-line interface for creating a superuser in the database. It supports both interactive prompts for user credentials and a non-interactive mode that utilizes command-line arguments or environment variables. The class automatically handles required model fields, password validation, and database selection.

Attributes

AttributeTypeDescription
helpstring = "Used to create a superuser."Used to create a superuser.
requires_migrations_checksboolean = trueIndicates whether the command should verify that all migrations have been applied before running.
stealth_optionstuple = ("stdin",)A tuple of options that are accepted by the command but not advertised in the help output.

Constructor

Signature

def Command(
*args: tuple,
**kwargs: dict
) - > null

Parameters

NameTypeDescription
*argstupleVariable length argument list passed to the base class constructor.
**kwargsdictArbitrary keyword arguments passed to the base class constructor.

Methods


add_arguments()

@classmethod
def add_arguments(
parser: ArgumentParser
)

Configures the command line argument parser with options for the username, database, and any required model fields.

Parameters

NameTypeDescription
parserArgumentParserThe argument parser instance to which superuser creation arguments will be added.

execute()

@classmethod
def execute(
*args: tuple,
**options: dict
) - > Any

Sets up the input stream for the command and executes the base command logic.

Parameters

NameTypeDescription
*argstupleVariable length argument list passed to the command.
**optionsdictCommand options including 'stdin' for input stream redirection.

Returns

TypeDescription
AnyThe result of the parent class's execute method.

handle()

@classmethod
def handle(
*args: tuple,
**options: dict
)

Executes the core logic for creating a superuser, handling both interactive terminal prompts and non-interactive environment-based creation.

Parameters

NameTypeDescription
*argstupleVariable length argument list.
**optionsdictParsed command line arguments including 'interactive', 'database', and field values.

get_input_data()

@classmethod
def get_input_data(
field: [Field](../../../../../forms/fields/field.md?sid=django_forms_fields_field),
message: string,
default: Any = None
) - > Any

Override this method if you want to customize data inputs or validation exceptions.

Parameters

NameTypeDescription
field[Field](../../../../../forms/fields/field.md?sid=django_forms_fields_field)The Django model field instance used to clean and validate the input.
messagestringThe prompt message displayed to the user.
defaultAny = NoneThe fallback value to use if the user provides no input.

Returns

TypeDescription
AnyThe cleaned value provided by the user, or the default value if input was empty.

username_is_unique()

@classmethod
def username_is_unique() - > boolean

Determines if the username field is constrained by a unique requirement or a single-field unique constraint.

Returns

TypeDescription
booleanTrue if the username must be unique in the database, False otherwise.