Skip to main content

Command

This class squashes an existing set of migrations, from a specified start point until a target migration, into a single new migration file. It provides functionality to optimize the resulting operations, handle dependencies, and generate the new migration script while maintaining a record of the replaced migrations. The class also supports interactive user confirmation and custom naming for the squashed output.

Attributes

AttributeTypeDescription
helpstringSquashes an existing set of migrations (from first until specified) into a single new one.

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 the app, migration range, and output formatting.

Parameters

NameTypeDescription
parser[CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser)The argument parser instance to which the squashmigration arguments will be added.

handle()

@classmethod
def handle(
**options: dict
) - > null

Executes the squashing logic by consolidating operations from a range of migrations into a single new migration file. This method validates the app and migrations, optionally optimizes operations, and writes the resulting migration to disk.

Parameters

NameTypeDescription
**optionsdictA dictionary of parsed command line arguments including 'app_label', 'migration_name', and 'no_optimize'.

Returns

TypeDescription
nullReturns null; output is written to the filesystem and stdout.

find_migration()

@classmethod
def find_migration(
loader: [MigrationLoader](../../../../db/migrations/loader/migrationloader.md?sid=django_db_migrations_loader_migrationloader),
app_label: str,
name: str
) - > [Migration](../../../../db/migrations/migration/migration.md?sid=django_db_migrations_migration_migration)

Retrieves a specific migration object from the loader based on a partial or full name prefix. Raises a CommandError if the name is ambiguous or if no matching migration is found for the specified app.

Parameters

NameTypeDescription
loader[MigrationLoader](../../../../db/migrations/loader/migrationloader.md?sid=django_db_migrations_loader_migrationloader)The migration loader instance containing the current migration graph.
app_labelstrThe name of the Django application the migration belongs to.
namestrThe name or prefix of the migration to locate.

Returns

TypeDescription
[Migration](../../../../db/migrations/migration/migration.md?sid=django_db_migrations_migration_migration)The migration instance matching the provided app label and name prefix.