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