Skip to main content

Command

This class introspects database tables and views to automatically generate a Django model module. It provides functionality to map database columns to model fields, handle relationships like foreign keys, and configure metadata such as table names and constraints. The class supports filtering specific tables and can optionally include database partitions and views in the output.

Attributes

AttributeTypeDescription
helpstring = "Introspects the database tables in the given database and outputs a Django model module."Introspects the database tables in the given database and outputs a Django model module.
requires_system_checkslist = []List of system checks that must be performed before the command can be executed.
stealth_optionstuple = ("table_name_filter",)Tuple of internal options that are not exposed to the command-line help but are used by the command logic.
db_modulestring = "django.db"The Python module path used to import Django models in the generated output.

Constructor

Signature

def 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 selecting specific tables, target databases, and including views or partitions.

Parameters

NameTypeDescription
parser[CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser)The argument parser instance to which introspect-specific arguments are added

handle()

@classmethod
def handle(
**options: dict
)

Executes the command logic, verifying that the database backend supports introspection before writing the generated model code to standard output.

Parameters

NameTypeDescription
**optionsdictThe parsed command-line arguments including database alias and table filters

handle_inspection()

@classmethod
def handle_inspection(
options: dict
) - > generator

Generates the Python source code for Django models by introspecting the database schema, including table structures, relationships, and constraints.

Parameters

NameTypeDescription
optionsdictConfiguration options determining which tables, views, or partitions to process

Returns

TypeDescription
generatorA generator yielding strings of Python code representing the auto-generated model module

normalize_col_name()

@classmethod
def normalize_col_name(
col_name: string,
used_column_names: list,
is_relation: boolean
) - > tuple

Modify the column name to make it Python-compatible as a field name

Parameters

NameTypeDescription
col_namestringThe raw column name from the database schema
used_column_nameslistA list of field names already assigned to the current model to prevent naming collisions
is_relationbooleanIndicates if the column represents a foreign key relationship

Returns

TypeDescription
tupleA tuple containing the sanitized field name, a dictionary of field parameters, and a list of explanatory notes

normalize_table_name()

@classmethod
def normalize_table_name(
table_name: string
) - > string

Translate the table name to a Python-compatible model name.

Parameters

NameTypeDescription
table_namestringThe raw database table name to be converted

Returns

TypeDescription
stringA PascalCase string suitable for use as a Python class name

get_field_type()

@classmethod
def get_field_type(
connection: [BaseDatabaseWrapper](../../../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper),
table_name: string,
row: object
) - > tuple

Given the database connection, the table name, and the cursor row description, this routine will return the given field type name, as well as any additional keyword parameters and notes for the field.

Parameters

NameTypeDescription
connection[BaseDatabaseWrapper](../../../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper)The active database connection used for introspection
table_namestringThe name of the table containing the field
rowobjectThe column description object containing type codes and metadata

Returns

TypeDescription
tupleA tuple containing the Django field class name, a dictionary of field arguments, and a list of notes

get_meta()

@classmethod
def get_meta(
table_name: string,
constraints: dict,
column_to_field_name: dict,
is_view: boolean,
is_partition: boolean,
comment: string
) - > list

Return a sequence comprising the lines of code necessary to construct the inner Meta class for the model corresponding to the given database table name.

Parameters

NameTypeDescription
table_namestringThe name of the database table
constraintsdictA dictionary of database constraints used to determine unique_together settings
column_to_field_namedictA mapping of database column names to their corresponding Python model field names
is_viewbooleanWhether the object is a database view
is_partitionbooleanWhether the object is a database partition
commentstringThe database comment associated with the table

Returns

TypeDescription
listA list of strings representing the lines of the generated Meta class