django_evolution.mutators.model_mutator

Mutator that applies changes to a model.

New in version 2.2.

Classes

ModelMutator(app_mutator, model_name)

Tracks and runs mutations for a model.

class django_evolution.mutators.model_mutator.ModelMutator(app_mutator, model_name)

Bases: BaseAppStateMutator

Tracks and runs mutations for a model.

A ModelMutator is bound to a particular model (by type, not instance) and handles operations that apply to that model.

Operations are first registered by mutations, and then later provided to the database’s operations backend, where they will be applied to the database.

After all operations are added, the caller is expected to call to_sql() to get the SQL statements needed to apply those operations. Once called, the mutator is finalized, and new operations cannot be added.

ModelMutator only works with mutations that are instances of BaseModelFieldMutation.

This is instantiated by AppMutator, and should not be created manually.

Changed in version 2.2: Moved into the django_evolution.mutators.model_mutator module.

__init__(app_mutator, model_name)

Initialize the mutator.

Parameters:
  • app_mutator (AppMutator) – The app mutator that owns this model mutator.

  • model_name (unicode) – The name of the model being evolved.

  • app_label (unicode) – The label of the app to evolve.

  • legacy_app_label (unicode) – The legacy label of the app to evolve. This is based on the module name and is used in the transitioning of pre-Django 1.7 signatures.

  • project_sig (django_evolution.signature.ProjectSignature) – The project signature being evolved.

  • database_state (django_evolution.db.state.DatabaseState) – The database state information to manipulate.

  • database (unicode, optional) – The name of the database being evolved.

property model_sig

The model signature that this mutator is working with.

Type:

django_evolution.signature.ModelSignature

Raises:

django_evolution.errors.EvolutionBaselineMissingError – The model signature or parent app signature could not be found.

create_model()

Create a mock model instance with the stored information.

This is typically used when calling a mutation’s mutate() function and passing a model instance, but can also be called whenever a new instance of the model is needed for any lookups.

Returns:

The resulting mock model.

Return type:

django_evolution.mock_models.MockModel

Raises:

django_evolution.errors.EvolutionBaselineMissingError – The model signature or parent app signature could not be found.

add_column(mutation, field, initial)

Adds a pending Add Column operation.

This will cause to_sql() to include SQL for adding the column with the given information to the model.

change_column_type(mutation, old_field, new_field, new_attrs)

Add a pending Change Column Type operation.

This will cause to_sql() to include SQL for changing a field to a new type.

Parameters:
  • mutation (django_evolution.mutations.ChangeField) – The mutation that triggered this column type change.

  • old_field (django.db.models.Field) – The old field on the model.

  • new_field (django.db.models.Field) – The new field on the model.

  • new_attrs (dict) – New attributes set in the ChangeField.

change_column(mutation, field, new_attrs)

Adds a pending Change Column operation.

This will cause to_sql() to include SQL for changing one or more attributes for the given column.

delete_column(mutation, field)

Adds a pending Delete Column operation.

This will cause to_sql() to include SQL for deleting the given column.

delete_model(mutation)

Adds a pending Delete Model operation.

This will cause to_sql() to include SQL for deleting the model.

change_meta(mutation, prop_name, new_value)

Adds a pending Change Meta operation.

This will cause to_sql() to include SQL for changing a supported attribute in the model’s Meta class.

add_sql(mutation, sql)

Adds an operation for executing custom SQL.

This will cause to_sql() to include the provided SQL statements. The SQL should be a list of a statements.

run_mutation(mutation)

Run the specified mutation.

The mutation will be provided with a temporary mock instance of a model that can be used for field or meta lookups.

The mutator must be finalized before this can be called.

Once the mutation has been run, it will call run_simulation(), applying changes to the database project signature.

Parameters:

mutation (django_evolution.mutations.BaseModelMutation) – The mutation to run.

Raises:

django_evolution.errors.EvolutionBaselineMissingError – The model signature or parent app signature could not be found.

to_sql()

Returns SQL for the operations added to this mutator.

The SQL will represent all the operations made by the mutator, as determined by the database operations backend.

Once called, no new operations can be added to the mutator.

finish_op(op)

Finishes handling an operation.

This is called by the evolution operations backend when it is done with an operation.

Simulations for the operation’s associated mutation will be applied, in order to update the signatures for the changes made by the mutation.

Parameters:

op (dict) – The operation that has finished.