django_evolution.utils.models

Utilities for working with models.

Functions

clear_model_rel_tree()

Clear the model relationship tree.

get_database_for_model_name(app_name, model_name)

Return the database used for a given model.

get_model_rel_tree()

Return the full field relationship tree for all registered models.

iter_model_fields(model[, ...])

Iterate through all fields on a model using the given criteria.

iter_non_m2m_reverse_relations(field)

Iterate through non-M2M reverse relations pointing to a field.

walk_model_tree(model)

Walk through a tree of models.

django_evolution.utils.models.get_database_for_model_name(app_name, model_name)

Return the database used for a given model.

Given an app name and a model name, this will return the proper database connection name used for making changes to that model. It will go through any custom routers that understand that type of model.

Parameters:
  • app_name (unicode) – The name of the app owning the model.

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

Returns:

The name of the database used for the model.

Return type:

unicode

django_evolution.utils.models.walk_model_tree(model)

Walk through a tree of models.

This will yield the provided model and its parents, in turn yielding their parents, and so on.

New in version 2.2.

Parameters:

model (type) – The top of the model tree to iterate through.

Yields:

type – Each model class in the tree.

django_evolution.utils.models.get_model_rel_tree()

Return the full field relationship tree for all registered models.

This will walk through every field in every model registered in Django, storing the relationships between objects, caching them. Each entry in the resulting dictionary will be a table mapping to a list of relation fields that point back at it.

This can be used to quickly locate any and all reverse relations made to a field.

This is similar to Django’s built-in reverse relation tree used internally (with different implementations) in django.db.models.options.Options, but works across all supported versions of Django, and supports cache clearing.

New in version 2.2.

Returns:

The model relation tree.

Return type:

dict

django_evolution.utils.models.clear_model_rel_tree()

Clear the model relationship tree.

This will cause the next call to get_model_rel_tree() to re-compute the full tree.

New in version 2.2.

django_evolution.utils.models.iter_model_fields(model, include_parent_models=True, include_forward_fields=True, include_reverse_fields=False, include_hidden_fields=False, seen_models=None)

Iterate through all fields on a model using the given criteria.

This is roughly equivalent to Django’s internal django.db.models.options.Option._get_fields() on Django 1.8+, but makes use of our model reverse relation tree, and works across all supported versions of Django.

New in version 2.2.

Parameters:
  • model (type) – The model owning the fields.

  • include_parent_models (bool, optional) – Whether to include fields defined on parent models.

  • include_forward_fields (bool, optional) – Whether to include fields owned by the model (or a parent).

  • include_reverse_fields (bool, optional) – Whether to include fields on other models that point to this model.

  • include_hidden_fields (bool, optional) – Whether to include hidden fields.

  • seen_models (set, optional) – Models seen during iteration. This is intended for internal use only by this function.

Yields:

django.db.models.Field – Each field matching the criteria.

django_evolution.utils.models.iter_non_m2m_reverse_relations(field)

Iterate through non-M2M reverse relations pointing to a field.

This will exclude any :py:class:`~django.db.models.ManyToManyField`s, but will include the relation fields on their “through” tables.

Note that this may return duplicate results, or multiple relations pointing to the same field. It’s up to the caller to handle this.

New in version 2.2.

Parameters:

field (django.db.models.Field) – The field that relations must point to.

Yields:

django.db.models.Field or object – Each field or relation object pointing to this field.

The type of the relation object depends on the version of Django.