Fix the compatibility with the OneToOneField class

This commit is contained in:
Pierre Dulac 2015-10-16 16:21:59 +02:00
parent 0a89478972
commit f172f0d43f
2 changed files with 26 additions and 6 deletions

View File

@ -95,6 +95,21 @@ else:
return opts.get_all_related_many_to_many_objects() 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 # django-filter is optional
try: try:
import django_filters import django_filters

View File

@ -14,7 +14,10 @@ from django.db import models
from django.utils import six from django.utils import six
from rest_framework.compat import ( 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', [ FieldInfo = namedtuple('FieldResult', [
@ -131,26 +134,28 @@ def _get_reverse_relationships(opts):
reverse_relations = OrderedDict() reverse_relations = OrderedDict()
for relation in get_all_related_objects(opts): 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) related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo( reverse_relations[accessor_name] = RelationInfo(
model_field=None, model_field=None,
related_model=related, related_model=related,
to_many=relation.field.rel.multiple, to_many=field.rel.multiple,
has_through_model=False has_through_model=False
) )
# Deal with reverse many-to-many relationships. # Deal with reverse many-to-many relationships.
for relation in get_all_related_many_to_many_objects(opts): 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) related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo( reverse_relations[accessor_name] = RelationInfo(
model_field=None, model_field=None,
related_model=related, related_model=related,
to_many=True, to_many=True,
has_through_model=( has_through_model=(
(getattr(relation.field.rel, 'through', None) is not None) and (getattr(field.rel, 'through', None) is not None) and
not relation.field.rel.through._meta.auto_created not field.rel.through._meta.auto_created
) )
) )