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
Return the default auto field type. |
|
Return the default auto field type. |
|
|
Return whether a field is hidden. |
|
Return whether a field is a Many-to-Many field. |
|
Return whether a field is a relation. |
|
Return the model's name. |
|
Return the models belonging to an app. |
|
Return the target field for a field's relation. |
|
Return the remote field for a relation. |
Return the model a relation is pointing to. |
|
Return the model a relation is pointing from. |
|
|
Set the name of a model. |
- exception django_evolution.compat.models.FieldDoesNotExist
Bases:
ExceptionThe 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,FieldProvide a generic many-to-one relation through the
content_typeandobject_idfields.This class also doubles as an accessor to the related object (similar to ForwardManyToOneDescriptor) by adding itself as a model attribute.
- many_to_many = False
- many_to_one = True
- one_to_many = False
- one_to_one = False
- __init__(ct_field='content_type', fk_field='object_id', for_concrete_model=True)
- 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.
- get_attname_column()
- get_filter_kwargs_for_object(obj)
See corresponding method on Field
See corresponding method on RelatedField
- check(**kwargs)
- cache_name
- get_content_type(obj=None, id=None, using=None, model=None)
- get_prefetch_queryset(instances, queryset=None)
- get_prefetch_querysets(instances, querysets=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:
ForeignObjectProvide a reverse to a relation created by a GenericForeignKey.
- auto_created = False
- empty_strings_allowed = 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(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
objsvia thisGenericRelation.
- __annotations__ = {}
- django_evolution.compat.models.get_default_auto_field()
Return the default auto field type.
Added in version 3.0.
- Returns:
The dotted-notation name of the class to use for models that don’t have a field with
primary_key=True.- Return type:
- django_evolution.compat.models.get_default_auto_field_cls()
Return the default auto field type.
Added in version 3.0.
- Returns:
The field class to use for models that don’t have a field with
primary_key=True.- Return type:
Return whether a field is hidden.
Added in version 2.2.
- Parameters:
field (
django.db.models.Field) – The field to check.- Returns:
Trueif the field is hidden.Falseif it is not.- Return type:
- django_evolution.compat.models.get_field_is_many_to_many(field)
Return whether a field is a Many-to-Many field.
Added in version 2.2.
- Parameters:
field (
django.db.models.Field) – The field to check.- Returns:
Trueif the field is a Many-to-Many field.Falseif it is not.- Return type:
- 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.ForeignKeyordjango.db.models.ManyToManyField, or if it’s a relation utility field likedjango.db.models.fields.related.ForeignObjectRelordjango.db.models.fields.related.ManyToOneRel.Added in version 2.2.
- Parameters:
field (
django.db.models.Fieldor :py:class:`` :py:class:`` :py:class:`` :py:class:`` :py:class:`` :py:class:`` :py:class:`` :py:class:`` :py:class:`` :py:class:`` :py:class:``django.db.models.fields.related.ForeignObjectRel) – The field to check.- Returns:
Trueif the field is a relation.Falseif it is not.- Return type:
- 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.
- 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:
- 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
ForeignKeyand 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_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.ForeignObjectReldjango.db.models.fields.related.ManyToOneReldjango.db.models.fields.related.OneToOneReldjango.db.models.fields.related.ManyToManyRel
This is equivalent to
relprior to Django 1.9 andremote_fieldin 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_evolution.compat.models.get_remote_field_model(rel)
Return the model a relation is pointing to.
This is equivalent to
rel.toprior to Django 1.9 andremote_field.modelin 1.9 onward.- Parameters:
rel (
object) – The relation object. This is expected to be the result of aget_remote_field()call.- Returns:
The model the relation points to. This should be a subclass of
django.db.models.Model().- Return type:
Return the model a relation is pointing from.
Added in version 2.2.
- Parameters:
rel (
object) – The relation object. This is expected to be the result of aget_remote_field()call.- Returns:
The model the relation points to. This should be a subclass of
django.db.models.Model().- Return type:
- django_evolution.compat.models.set_model_name(model, name)
Set the name of a model.
- Parameters:
model (
django.db.models.Model) – The model to set the new name on.name (
str) – The new model name.