Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Added a compatibility function to support versions prior to Django 1.9
  • Loading branch information
jplock committed Feb 6, 2016
commit 83e73cd5bb6d0518880d92677c7e0a44918d5a51
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
23 changes: 12 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,16 @@ def get_field_info(model):

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we instead have this set in a var after line 83? remote_field = get_remote_field(pk) and use throughout?


return pk


def _get_fields(opts):
fields = OrderedDict()
for field in [field for field in opts.fields if field.serialize and not field.remote_field]:
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,10 +105,10 @@ 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.remote_field]:
for field in [field for field in opts.fields if field.serialize and get_remote_field(field)]:
forward_relations[field.name] = RelationInfo(
model_field=field,
related_model=_resolve_model(field.remote_field.to),
related_model=_resolve_model(get_remote_field(field).to),
to_many=False,
to_field=_get_to_field(field),
has_through_model=False
Expand All @@ -117,12 +118,12 @@ def _get_forward_relationships(opts):
for field in [field for field in opts.many_to_many if field.serialize]:
forward_relations[field.name] = RelationInfo(
model_field=field,
related_model=_resolve_model(field.remote_field.to),
related_model=_resolve_model(get_remote_field(field).to),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

to_many=True,
# manytomany do not have to_fields
to_field=None,
has_through_model=(
not field.remote_field.through._meta.auto_created
not get_remote_field(field).through._meta.auto_created
)
)

Expand All @@ -144,7 +145,7 @@ def _get_reverse_relationships(opts):
reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related_model=related,
to_many=relation.field.remote_field.multiple,
to_many=get_remote_field(relation.field).multiple,
to_field=_get_to_field(relation.field),
has_through_model=False
)
Expand All @@ -160,8 +161,8 @@ def _get_reverse_relationships(opts):
# manytomany do not have to_fields
to_field=None,
has_through_model=(
(getattr(relation.field.remote_field, 'through', None) is not None) and
not relation.field.remote_field.through._meta.auto_created
(getattr(get_remote_field(relation.field), 'through', None) is not None) and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

not get_remote_field(relation.field).through._meta.auto_created
)
)

Expand Down