Skip to main content

Command

This class creates new migration files based on changes detected in your application models. It provides capabilities to package model changes into new migration files, merge conflicting migration branches, and generate empty migrations for custom data or schema changes. The class supports various execution modes including dry runs, scriptable output for automation, and consistency checks against the current database state.

Attributes

AttributeTypeDescription
autodetectortype = MigrationAutodetectorThe class used to detect changes between the current project state and the migration history.
helpstring = "Creates new migration(s) for apps."Creates new migration(s) for apps.

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 app filtering, dry runs, merging conflicts, and output formatting.

Parameters

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

log_output()

@classmethod
def log_output() - > file-like object

Determines the appropriate output stream based on whether scriptable mode is enabled.

Returns

TypeDescription
file-like objectThe stderr stream if scriptable mode is active, otherwise the stdout stream.

log()

@classmethod
def log(
msg: str
)

Writes a message to the designated log output stream.

Parameters

NameTypeDescription
msgstrThe text message to be written to the output stream.

handle()

@classmethod
def handle(
app_labels: str,
options: dict
)

Executes the core logic for detecting model changes and generating migration files or handling merges.

Parameters

NameTypeDescription
app_labelsstrOptional specific application labels to limit the migration detection scope.
optionsdictA dictionary of parsed command-line arguments and configuration flags.

write_to_last_migration_files()

@classmethod
def write_to_last_migration_files(
changes: dict
)

Updates the most recent migration file for each app by appending new operations and optimizing the result.

Parameters

NameTypeDescription
changesdictA dictionary mapping app labels to lists of detected migration changes.

write_migration_files()

@classmethod
def write_migration_files(
changes: dict,
update_previous_migration_paths: dict = None
)

Take a changes dict and write them out as migration files.

Parameters

NameTypeDescription
changesdictA dictionary mapping app labels to the migration objects that need to be serialized to disk.
update_previous_migration_pathsdict = NoneA mapping of app labels to file paths of previous migrations that should be deleted after a successful update.

get_relative_path()

@classmethod
def get_relative_path(
path: str
) - > str

Calculates a relative file path from the current working directory, falling back to the absolute path if the relative path is outside the project root.

Parameters

NameTypeDescription
pathstrThe absolute file system path to be converted.

Returns

TypeDescription
strThe most concise file path representation for display in logs.

handle_merge()

@classmethod
def handle_merge(
loader: [MigrationLoader](../../../../db/migrations/loader/migrationloader.md?sid=django_db_migrations_loader_migrationloader),
conflicts: dict
)

Handles merging together conflicted migrations interactively, if it's safe; otherwise, advises on how to fix it.

Parameters

NameTypeDescription
loader[MigrationLoader](../../../../db/migrations/loader/migrationloader.md?sid=django_db_migrations_loader_migrationloader)The loader instance containing the current migration graph and disk state.
conflictsdictA dictionary of app labels and their corresponding conflicting migration names.