django_evolution.compat.models

Compatibility functions for model-related operations.

This provides functions for working with models or importing moved fields. These translate to the various versions of Django that are supported.

Functions

get_field_is_hidden(field)

Return whether a field is hidden.

get_field_is_many_to_many(field)

Return whether a field is a Many-to-Many field.

get_field_is_relation(field)

Return whether a field is a relation.

get_model_name(model)

Return the model's name.

get_models([app_mod, include_auto_created])

Return the models belonging to an app.

get_rel_target_field(field)

Return the target field for a field's relation.

get_remote_field(field)

Return the remote field for a relation.

get_remote_field_model(rel)

Return the model a relation is pointing to.

get_remote_field_related_model(rel)

Return the model a relation is pointing from.

set_model_name(model, name)

Set the name of a model.

exception django_evolution.compat.models.FieldDoesNotExist

Bases: Exception

The requested model field does not exist

class django_evolution.compat.models.GenericForeignKey(ct_field='content_type', fk_field='object_id', for_concrete_model=True)

Bases: FieldCacheMixin

Provide a generic many-to-one relation through the content_type and object_id fields.

This class also doubles as an accessor to the related object (similar to ForwardManyToOneDescriptor) by adding itself as a model attribute.

auto_created = False
concrete = False
hidden = False
is_relation = True
many_to_many = False
many_to_one = True
one_to_many = False
one_to_one = False
related_model = None
remote_field = None
__init__(ct_field='content_type', fk_field='object_id', for_concrete_model=True)
editable = False
contribute_to_class(cls, name, **kwargs)
get_filter_kwargs_for_object(obj)

See corresponding method on Field

See corresponding method on RelatedField

__str__()

Return str(self).

check(**kwargs)
get_cache_name()
get_content_type(obj=None, id=None, using=None)
get_prefetch_queryset(instances, queryset=None)
__get__(instance, cls=None)
__set__(instance, value)
class django_evolution.compat.models.GenericRelation(to, object_id_field='object_id', content_type_field='content_type', for_concrete_model=True, related_query_name=None, limit_choices_to=None, **kwargs)

Bases: ForeignObject

Provide a reverse to a relation created by a GenericForeignKey.

auto_created = False
many_to_many = False
many_to_one = False
one_to_many = True
one_to_one = False
rel_class

alias of GenericRel

mti_inherited = False
__init__(to, object_id_field='object_id', content_type_field='content_type', for_concrete_model=True, related_query_name=None, limit_choices_to=None, **kwargs)
check(**kwargs)
get_path_info(filtered_relation=None)

Get path from this field to the related model.

get_reverse_path_info(filtered_relation=None)

Get path from the related model to this field’s model.

value_to_string(obj)

Return a string value of this field from the passed obj. This is used by the serialization framework.

contribute_to_class(cls, name, **kwargs)

Register the field with the model class it belongs to.

If private_only is True, create a separate instance of this field for every subclass of cls, even if cls is not an abstract model.

set_attributes_from_rel()
get_internal_type()
get_content_type()

Return the content type associated with this field’s model.

get_extra_restriction(where_class, alias, remote_alias)

Return a pair condition used for joining and subquery pushdown. The condition is something that responds to as_sql(compiler, connection) method.

Note that currently referring both the ‘alias’ and ‘related_alias’ will not work in some conditions, like subquery pushdown.

A parallel method is get_extra_descriptor_filter() which is used in instance.fieldname related object fetching.

Return all objects related to objs via this GenericRelation.

django_evolution.compat.models.get_field_is_hidden(field)

Return whether a field is hidden.

New in version 2.2.

Parameters:

field (django.db.models.Field) – The field to check.

Returns:

True if the field is hidden. False if it is not.

Return type:

bool

django_evolution.compat.models.get_field_is_many_to_many(field)

Return whether a field is a Many-to-Many field.

New in version 2.2.

Parameters:

field (django.db.models.Field) – The field to check.

Returns:

True if the field is a Many-to-Many field. False if it is not.

Return type:

bool

django_evolution.compat.models.get_field_is_relation(field)

Return whether a field is a relation.

A field is a relation if it’s an object like a django.db.models.ForeignKey or django.db.models.ManyToManyField, or if it’s a relation utility field like django.db.models.fields.related.ForeignObjectRel or django.db.models.fields.related.ManyToOneRel.

New in version 2.2.

Parameters:

field (django.db.models.Field or :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` :class:`` django.db.models.fields.related.ForeignObjectRel) – The field to check.

Returns:

True if the field is a relation. False if it is not.

Return type:

bool

django_evolution.compat.models.get_model(app_label, model_name=None, require_ready=True)

Return the model matching the given app_label and model_name.

As a shortcut, app_label may be in the form <app_label>.<model_name>.

model_name is case-insensitive.

Raise LookupError if no application exists with this label, or no model exists with this name in the application. Raise ValueError if called with a single argument that doesn’t contain exactly one dot.

django_evolution.compat.models.get_models(app_mod=None, include_auto_created=False)

Return the models belonging to an app.

Parameters:
  • app_mod (module, optional) – The application module.

  • include_auto_created (bool, optional) – Whether to return auto-created models (such as many-to-many models) in the results.

Returns:

The list of modules belonging to the app.

Return type:

list

django_evolution.compat.models.get_model_name(model)

Return the model’s name.

Parameters:

model (django.db.models.Model) – The model for which to return the name.

Returns:

The model’s name.

Return type:

str

django_evolution.compat.models.get_rel_target_field(field)

Return the target field for a field’s relation.

Warning

Despite the name, this should only be called on a ForeignKey and not on a relation, in order to avoid consistency issues in the data returned on Django >= 1.7.

Parameters:

field (django.db.models.Field) – The relation field.

Returns:

The field on the other end of the relation.

Return type:

django.db.models.Field

django_evolution.compat.models.get_remote_field(field)

Return the remote field for a relation.

This will be an intermediary field, such as:

  • django.db.models.fields.related.ForeignObjectRel

  • django.db.models.fields.related.ManyToOneRel

  • django.db.models.fields.related.OneToOneRel

  • django.db.models.fields.related.ManyToManyRel

This is equivalent to rel prior to Django 1.9 and remote_field in 1.9 onward.

Changed in version 2.2: On Django < 1.9, a main relation field (like django.db.models.ForeignKey) will return the utility relation, matching the behavior on >= 1.9.

Parameters:

field (django.db.models.Field) – The relation field.

Returns:

The remote field on the relation.

Return type:

django.db.models.Field

django_evolution.compat.models.get_remote_field_model(rel)

Return the model a relation is pointing to.

This is equivalent to rel.to prior to Django 1.9 and remote_field.model in 1.9 onward.

Parameters:

rel (object) – The relation object. This is expected to be the result of a get_remote_field() call.

Returns:

The model the relation points to. This should be a subclass of django.db.models.Model().

Return type:

type

Return the model a relation is pointing from.

New in version 2.2.

Parameters:

rel (object) – The relation object. This is expected to be the result of a get_remote_field() call.

Returns:

The model the relation points to. This should be a subclass of django.db.models.Model().

Return type:

type

django_evolution.compat.models.set_model_name(model, name)

Set the name of a model.

Parameters: