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
Fix the compatibility with the OneToOneField class
  • Loading branch information
dulacp committed Oct 16, 2015
commit f172f0d43f49001698de0fb0bb3a7a0cd750bee0
15 changes: 15 additions & 0 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ def get_all_related_many_to_many_objects(opts):
return opts.get_all_related_many_to_many_objects()


# Compatibility for the *field* instance returned by either
# the old `Options.get_all_related_objects` or our own implementation above
def get_relation_accessor_name(relation):
if not hasattr(relation, 'get_accessor_name'):
# special case for the `OneToOneField` instances
return relation.name
return relation.get_accessor_name()

def get_relation_field(relation):
if not hasattr(relation, 'get_accessor_name'):
# special case for the `OneToOneField` instances
return relation
return relation.field


# django-filter is optional
try:
import django_filters
Expand Down
17 changes: 11 additions & 6 deletions rest_framework/utils/model_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
from django.utils import six

from rest_framework.compat import (
get_all_related_objects, get_all_related_many_to_many_objects
get_all_related_objects,
get_all_related_many_to_many_objects,
get_relation_accessor_name,
get_relation_field
)

FieldInfo = namedtuple('FieldResult', [
Expand Down Expand Up @@ -131,26 +134,28 @@ def _get_reverse_relationships(opts):

reverse_relations = OrderedDict()
for relation in get_all_related_objects(opts):
accessor_name = relation.get_accessor_name()
accessor_name = get_relation_accessor_name(relation)
field = get_relation_field(relation)
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related_model=related,
to_many=relation.field.rel.multiple,
to_many=field.rel.multiple,
has_through_model=False
)

# Deal with reverse many-to-many relationships.
for relation in get_all_related_many_to_many_objects(opts):
accessor_name = relation.get_accessor_name()
accessor_name = get_relation_accessor_name(relation)
field = get_relation_field(relation)
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related_model=related,
to_many=True,
has_through_model=(
(getattr(relation.field.rel, 'through', None) is not None) and
not relation.field.rel.through._meta.auto_created
(getattr(field.rel, 'through', None) is not None) and
not field.rel.through._meta.auto_created
)
)

Expand Down