Command
This class provides a management command to create the database tables required for a SQL cache backend. It can automatically identify cache tables from project settings or accept specific table names as arguments. The class supports targeting specific databases and includes a dry-run mode to preview the generated SQL without executing it.
Attributes
| Attribute | Type | Description |
|---|---|---|
| help | string = "Creates the tables needed to use the SQL cache backend." | Creates the tables needed to use the SQL cache backend. |
| requires_system_checks | list = [] | A list of system checks that must be performed before the command is executed, which is empty for this command. |
Methods
add_arguments()
@classmethod
def add_arguments(
parser: [CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser)
)
Configures the command line argument parser with options for table names, database selection, and dry-run mode.
Parameters
| Name | Type | Description |
|---|---|---|
| parser | [CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser) | The argument parser instance to which the cache table configuration arguments will be added. |
handle()
@classmethod
def handle(
*tablenames: string,
**options: dict
)
Executes the command logic to identify and create cache tables based on provided arguments or the project's cache settings.
Parameters
| Name | Type | Description |
|---|---|---|
| *tablenames | string | Optional positional arguments specifying specific table names to create. |
| **options | dict | A dictionary of command options including 'database', 'dry_run', and 'verbosity'. |
create_table()
@classmethod
def create_table(
database: string,
tablename: string,
dry_run: boolean
)
Generates and executes the SQL required to create a specific cache table and its associated indexes on the target database.
Parameters
| Name | Type | Description |
|---|---|---|
| database | string | The alias of the database connection where the table should be created. |
| tablename | string | The name of the database table to be created for the cache backend. |
| dry_run | boolean | If true, the method prints the SQL statements to stdout instead of executing them against the database. |