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
| Attribute | Type | Description |
|---|
| autodetector | type = MigrationAutodetector | The class used to detect changes between the current project state and the migration history. |
| help | string = "Creates new migration(s) for apps." | Creates new migration(s) for apps. |
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 app filtering, dry runs, merging conflicts, and output formatting.
Parameters
| Name | Type | Description |
|---|
| 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
| Type | Description |
|---|
file-like object | The 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
| Name | Type | Description |
|---|
| msg | str | The 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
| Name | Type | Description |
|---|
| app_labels | str | Optional specific application labels to limit the migration detection scope. |
| options | dict | A 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
| Name | Type | Description |
|---|
| changes | dict | A 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
| Name | Type | Description |
|---|
| changes | dict | A dictionary mapping app labels to the migration objects that need to be serialized to disk. |
| update_previous_migration_paths | dict = None | A 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
| Name | Type | Description |
|---|
| path | str | The absolute file system path to be converted. |
Returns
| Type | Description |
|---|
str | The 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
| Name | Type | Description |
|---|
| loader | [MigrationLoader](../../../../db/migrations/loader/migrationloader.md?sid=django_db_migrations_loader_migrationloader) | The loader instance containing the current migration graph and disk state. |
| conflicts | dict | A dictionary of app labels and their corresponding conflicting migration names. |