22Utils.
33"""
44import copy
5+ import inspect
56import warnings
67from collections import OrderedDict
7- import inspect
88
99import inflection
10+ from rest_framework import exceptions
11+ from rest_framework .exceptions import APIException
12+
13+ import django
1014from django .conf import settings
11- from django .utils import encoding
12- from django .utils import six
15+ from django .db . models import Manager
16+ from django .utils import encoding , six
1317from django .utils .module_loading import import_string as import_class_from_dotted_path
1418from django .utils .translation import ugettext_lazy as _
15- from django .db .models import Manager
16- from rest_framework .exceptions import APIException
17- from rest_framework import exceptions
1819
1920try :
2021 from rest_framework .serializers import ManyRelatedField
2627except ImportError :
2728 HyperlinkedRouterField = type (None )
2829
30+ if django .VERSION >= (1 , 9 ):
31+ from django .db .models .fields .related_descriptors import ManyToManyDescriptor , ReverseManyToOneDescriptor
32+ ReverseManyRelatedObjectsDescriptor = type (None )
33+ else :
34+ from django .db .models .fields .related import ManyRelatedObjectsDescriptor as ManyToManyDescriptor
35+ from django .db .models .fields .related import ForeignRelatedObjectsDescriptor as ReverseManyToOneDescriptor
36+ from django .db .models .fields .related import ReverseManyRelatedObjectsDescriptor
37+
2938
3039def get_resource_name (context ):
3140 """
@@ -87,6 +96,7 @@ def get_serializer_fields(serializer):
8796 pass
8897 return fields
8998
99+
90100def format_keys (obj , format_type = None ):
91101 """
92102 Takes either a dict or list and returns it with camelized keys only if
@@ -148,6 +158,7 @@ def format_relation_name(value, format_type=None):
148158 pluralize = getattr (settings , 'JSON_API_PLURALIZE_RELATION_TYPE' , None )
149159 return format_resource_type (value , format_type , pluralize )
150160
161+
151162def format_resource_type (value , format_type = None , pluralize = None ):
152163 if format_type is None :
153164 format_type = getattr (settings , 'JSON_API_FORMAT_TYPES' , False )
@@ -167,7 +178,6 @@ def get_related_resource_type(relation):
167178 return get_resource_type_from_serializer (relation )
168179 except AttributeError :
169180 pass
170-
171181 relation_model = None
172182 if hasattr (relation , '_meta' ):
173183 relation_model = relation ._meta .model
@@ -184,7 +194,7 @@ def get_related_resource_type(relation):
184194 elif hasattr (parent_serializer , 'parent' ) and hasattr (parent_serializer .parent , 'Meta' ):
185195 parent_model = getattr (parent_serializer .parent .Meta , 'model' , None )
186196
187- if parent_model is not None :
197+ if parent_model is not None :
188198 if relation .source :
189199 if relation .source != '*' :
190200 parent_model_relation = getattr (parent_model , relation .source )
@@ -193,17 +203,17 @@ def get_related_resource_type(relation):
193203 else :
194204 parent_model_relation = getattr (parent_model , parent_serializer .field_name )
195205
196- if hasattr (parent_model_relation , 'related' ):
197- try :
206+ if type (parent_model_relation ) is ReverseManyToOneDescriptor :
207+ if django .VERSION >= (1 , 9 ):
208+ relation_model = parent_model_relation .rel .related_model
209+ elif django .VERSION >= (1 , 8 ):
198210 relation_model = parent_model_relation .related .related_model
199- except AttributeError :
200- # Django 1.7
211+ else :
201212 relation_model = parent_model_relation .related .model
202- elif hasattr (parent_model_relation , 'field' ):
203- try :
204- relation_model = parent_model_relation .field .remote_field .model
205- except AttributeError :
206- relation_model = parent_model_relation .field .related .model
213+ elif type (parent_model_relation ) is ManyToManyDescriptor :
214+ relation_model = parent_model_relation .field .remote_field .model
215+ elif type (parent_model_relation ) is ReverseManyRelatedObjectsDescriptor :
216+ relation_model = parent_model_relation .field .related .model
207217 else :
208218 return get_related_resource_type (parent_model_relation )
209219
0 commit comments