Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to address Django 2.0 deprecate warnings related to field.rel #3906

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,16 @@ def get_all_related_many_to_many_objects(opts):
return opts.get_all_related_many_to_many_objects()
else:
return [r for r in opts.related_objects if r.field.many_to_many]

def get_remote_field(field):
"""
Django 1.9 removed usage of Rel objects, see
https://github.com/django/django/pull/4241

:param field: Field
:return: remote field
"""
if django.VERSION < (1, 9):
return field.rel
else:
return field.remote_field
28 changes: 17 additions & 11 deletions rest_framework/utils/model_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from django.utils import six

from rest_framework.compat import (
get_all_related_many_to_many_objects, get_all_related_objects
get_all_related_many_to_many_objects, get_all_related_objects,
get_remote_field
)

FieldInfo = namedtuple('FieldResult', [
Expand Down Expand Up @@ -80,16 +81,17 @@ def get_field_info(model):

def _get_pk(opts):
pk = opts.pk
while pk.rel and pk.rel.parent_link:
remote_field = get_remote_field(pk)
while remote_field and remote_field.parent_link:
# If model is a child via multi-table inheritance, use parent's pk.
pk = pk.rel.to._meta.pk
pk = remote_field.to._meta.pk

return pk


def _get_fields(opts):
fields = OrderedDict()
for field in [field for field in opts.fields if field.serialize and not field.rel]:
for field in [field for field in opts.fields if field.serialize and not get_remote_field(field)]:
fields[field.name] = field

return fields
Expand All @@ -104,25 +106,27 @@ def _get_forward_relationships(opts):
Returns an `OrderedDict` of field names to `RelationInfo`.
"""
forward_relations = OrderedDict()
for field in [field for field in opts.fields if field.serialize and field.rel]:
for field in [field for field in opts.fields if field.serialize and get_remote_field(field)]:
remote_field = get_remote_field(field)
forward_relations[field.name] = RelationInfo(
model_field=field,
related_model=_resolve_model(field.rel.to),
related_model=_resolve_model(remote_field.to),
to_many=False,
to_field=_get_to_field(field),
has_through_model=False
)

# Deal with forward many-to-many relationships.
for field in [field for field in opts.many_to_many if field.serialize]:
remote_field = get_remote_field(field)
forward_relations[field.name] = RelationInfo(
model_field=field,
related_model=_resolve_model(field.rel.to),
related_model=_resolve_model(remote_field.to),
to_many=True,
# manytomany do not have to_fields
to_field=None,
has_through_model=(
not field.rel.through._meta.auto_created
not remote_field.through._meta.auto_created
)
)

Expand All @@ -141,10 +145,11 @@ def _get_reverse_relationships(opts):
for relation in get_all_related_objects(opts):
accessor_name = relation.get_accessor_name()
related = getattr(relation, 'related_model', relation.model)
remote_field = get_remote_field(relation.field)
reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related_model=related,
to_many=relation.field.rel.multiple,
to_many=remote_field.multiple,
to_field=_get_to_field(relation.field),
has_through_model=False
)
Expand All @@ -153,15 +158,16 @@ def _get_reverse_relationships(opts):
for relation in get_all_related_many_to_many_objects(opts):
accessor_name = relation.get_accessor_name()
related = getattr(relation, 'related_model', relation.model)
remote_field = get_remote_field(relation.field)
reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related_model=related,
to_many=True,
# manytomany do not have to_fields
to_field=None,
has_through_model=(
(getattr(relation.field.rel, 'through', None) is not None) and
not relation.field.rel.through._meta.auto_created
(getattr(remote_field, 'through', None) is not None) and
not remote_field.through._meta.auto_created
)
)

Expand Down