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
| Attribute | Type | Description |
|---|
| help | string = "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_checks | list = [] | List of system checks that must be performed before the command can be executed. |
| stealth_options | tuple = ("table_name_filter",) | Tuple of internal options that are not exposed to the command-line help but are used by the command logic. |
| db_module | string = "django.db" | The Python module path used to import Django models in the generated output. |
Constructor
Signature
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
| Name | Type | Description |
|---|
| 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
| Name | Type | Description |
|---|
| **options | dict | The 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
| Name | Type | Description |
|---|
| options | dict | Configuration options determining which tables, views, or partitions to process |
Returns
| Type | Description |
|---|
generator | A 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
| Name | Type | Description |
|---|
| col_name | string | The raw column name from the database schema |
| used_column_names | list | A list of field names already assigned to the current model to prevent naming collisions |
| is_relation | boolean | Indicates if the column represents a foreign key relationship |
Returns
| Type | Description |
|---|
tuple | A 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
| Name | Type | Description |
|---|
| table_name | string | The raw database table name to be converted |
Returns
| Type | Description |
|---|
string | A 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
| Name | Type | Description |
|---|
| connection | [BaseDatabaseWrapper](../../../../db/backends/base/base/basedatabasewrapper.md?sid=django_db_backends_base_base_basedatabasewrapper) | The active database connection used for introspection |
| table_name | string | The name of the table containing the field |
| row | object | The column description object containing type codes and metadata |
Returns
| Type | Description |
|---|
tuple | A tuple containing the Django field class name, a dictionary of field arguments, and a list of notes |
@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
| Name | Type | Description |
|---|
| table_name | string | The name of the database table |
| constraints | dict | A dictionary of database constraints used to determine unique_together settings |
| column_to_field_name | dict | A mapping of database column names to their corresponding Python model field names |
| is_view | boolean | Whether the object is a database view |
| is_partition | boolean | Whether the object is a database partition |
| comment | string | The database comment associated with the table |
Returns
| Type | Description |
|---|
list | A list of strings representing the lines of the generated Meta class |