@@ -168,7 +168,9 @@ def build_json_resource_obj(fields, resource, resource_instance, resource_name):
168
168
169
169
170
170
def get_related_resource_type (relation ):
171
- if hasattr (relation , 'get_queryset' ) and relation .get_queryset () is not None :
171
+ if hasattr (relation , '_meta' ):
172
+ relation_model = relation ._meta .model
173
+ elif hasattr (relation , 'get_queryset' ) and relation .get_queryset () is not None :
172
174
relation_model = relation .get_queryset ().model
173
175
else :
174
176
parent_serializer = relation .parent
@@ -270,11 +272,10 @@ def extract_relationships(fields, resource, resource_instance):
270
272
271
273
if isinstance (field , ManyRelatedField ):
272
274
relation_data = list ()
273
- related_object = field .child_relation
274
- relation_type = get_related_resource_type (related_object )
275
275
for related_object in relation_instance_or_manager .all ():
276
+ related_object_type = get_related_resource_type (related_object )
276
277
relation_data .append (OrderedDict ([
277
- ('type' , relation_type ),
278
+ ('type' , related_object_type ),
278
279
('id' , encoding .force_text (related_object .pk ))
279
280
]))
280
281
data .update ({
@@ -289,20 +290,18 @@ def extract_relationships(fields, resource, resource_instance):
289
290
290
291
if isinstance (field , ListSerializer ):
291
292
relation_data = list ()
292
- serializer = field .child
293
- relation_model = serializer .Meta .model
294
- relation_type = format_relation_name (relation_model .__name__ )
295
293
296
294
serializer_data = resource .get (field_name )
297
295
resource_instance_queryset = relation_instance_or_manager .all ()
298
296
if isinstance (serializer_data , list ):
299
297
for position in range (len (serializer_data )):
300
298
nested_resource_instance = resource_instance_queryset [position ]
301
- relation_data .append (
302
- OrderedDict (
303
- [('type' , relation_type ), ('id' , encoding .force_text (nested_resource_instance .pk ))]
304
- )
305
- )
299
+ nested_resource_instance_type = get_related_resource_type (
300
+ nested_resource_instance )
301
+ relation_data .append (OrderedDict ([
302
+ ('type' , nested_resource_instance_type ),
303
+ ('id' , encoding .force_text (nested_resource_instance .pk ))
304
+ ]))
306
305
307
306
data .update ({field_name : {'data' : relation_data }})
308
307
continue
0 commit comments