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
| Attribute | Type | Description |
|---|---|---|
| help | string = "Used to create a superuser." | Used to create a superuser. |
| requires_migrations_checks | boolean = true | Indicates whether the command should verify that all migrations have been applied before running. |
| stealth_options | tuple = ("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
| Name | Type | Description |
|---|---|---|
| *args | tuple | Variable length argument list passed to the base class constructor. |
| **kwargs | dict | Arbitrary 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
| Name | Type | Description |
|---|---|---|
| parser | ArgumentParser | The 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
| Name | Type | Description |
|---|---|---|
| *args | tuple | Variable length argument list passed to the command. |
| **options | dict | Command options including 'stdin' for input stream redirection. |
Returns
| Type | Description |
|---|---|
Any | The 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
| Name | Type | Description |
|---|---|---|
| *args | tuple | Variable length argument list. |
| **options | dict | Parsed 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
| Name | Type | Description |
|---|---|---|
| field | [Field](../../../../../forms/fields/field.md?sid=django_forms_fields_field) | The Django model field instance used to clean and validate the input. |
| message | string | The prompt message displayed to the user. |
| default | Any = None | The fallback value to use if the user provides no input. |
Returns
| Type | Description |
|---|---|
Any | The 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
| Type | Description |
|---|---|
boolean | True if the username must be unique in the database, False otherwise. |