liveBook · Manning

Apps.get_model()

As explained in Django in Action

apps.get_model() retrieves a model representation corresponding to a particular historical migration state.

apps.get_model() retrieves the model representation belonging to a particular historical migration state. That matters because importing the current model can expose fields and behavior that did not exist when the migration runs. section 10.2

Use it inside a RunPython function, whose required arguments are apps and schema_editor. Pass the application label and model name, then work with the returned model through its manager. section 10.2

Transforming Promoter data

Promoter = apps.get_model("promoters", "Promoter") retrieves the historical Promoter model. Iterating over Promoter.objects.all() can then populate full_name and common_name before saving each record. A reverse function retrieves the same historical model to restore the earlier fields.

How Django in Action covers it

How deep it goes
Explains the lookup and migration callback context, but does not detail schema-editor internals.
Examples
Demonstrates forward and backward Promoter name transformations using a data migration.
What it assumes
Assumes basic Django models and migrations while explaining why current-model imports are unsafe during migration execution.
What you can do after
Apply it via Django's RunPython to read and update records against the correct migration-era model.
How it connects
Connects historical models with Django migrations, RunPython, model managers, and schema editors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.