@@ -114,10 +114,14 @@ def extract_relationships(cls, fields, resource, resource_instance):
114
114
115
115
# Skip fields without relations
116
116
if not isinstance (
117
- field , (relations .RelatedField , relations .ManyRelatedField , BaseSerializer )
117
+ field , (relations .RelatedField , relations .ManyRelatedField )
118
118
):
119
119
continue
120
120
121
+ if isinstance (field , BaseSerializer ) and \
122
+ rendering_strategy == ATTRIBUTE_RENDERING_STRATEGY :
123
+ continue
124
+
121
125
source = field .source
122
126
relation_type = utils .get_related_resource_type (field )
123
127
@@ -252,56 +256,55 @@ def extract_relationships(cls, fields, resource, resource_instance):
252
256
})
253
257
continue
254
258
255
- if rendering_strategy == RELATIONS_RENDERING_STRATEGY :
256
- if isinstance (field , ListSerializer ):
259
+ if isinstance (field , ListSerializer ):
260
+ resolved , relation_instance = utils .get_relation_instance (
261
+ resource_instance , source , field .parent
262
+ )
263
+ if not resolved :
264
+ continue
265
+
266
+ relation_data = list ()
267
+
268
+ serializer_data = resource .get (field_name )
269
+ resource_instance_queryset = list (relation_instance )
270
+ if isinstance (serializer_data , list ):
271
+ for position in range (len (serializer_data )):
272
+ nested_resource_instance = resource_instance_queryset [position ]
273
+ nested_resource_instance_type = (
274
+ relation_type or
275
+ utils .get_resource_type_from_instance (nested_resource_instance )
276
+ )
277
+
278
+ relation_data .append (OrderedDict ([
279
+ ('type' , nested_resource_instance_type ),
280
+ ('id' , encoding .force_str (nested_resource_instance .pk ))
281
+ ]))
282
+
283
+ data .update ({field_name : {'data' : relation_data }})
284
+ continue
285
+
286
+ if isinstance (field , Serializer ):
287
+ relation_instance_id = getattr (resource_instance , source + "_id" , None )
288
+ if not relation_instance_id :
257
289
resolved , relation_instance = utils .get_relation_instance (
258
290
resource_instance , source , field .parent
259
291
)
260
292
if not resolved :
261
293
continue
262
294
263
- relation_data = list ()
264
-
265
- serializer_data = resource .get (field_name )
266
- resource_instance_queryset = list (relation_instance )
267
- if isinstance (serializer_data , list ):
268
- for position in range (len (serializer_data )):
269
- nested_resource_instance = resource_instance_queryset [position ]
270
- nested_resource_instance_type = (
271
- relation_type or
272
- utils .get_resource_type_from_instance (nested_resource_instance )
273
- )
295
+ if relation_instance is not None :
296
+ relation_instance_id = relation_instance .pk
274
297
275
- relation_data .append (OrderedDict ([
276
- ('type' , nested_resource_instance_type ),
277
- ('id' , encoding .force_str (nested_resource_instance .pk ))
278
- ]))
279
-
280
- data .update ({field_name : {'data' : relation_data }})
281
- continue
282
-
283
- if isinstance (field , Serializer ):
284
- relation_instance_id = getattr (resource_instance , source + "_id" , None )
285
- if not relation_instance_id :
286
- resolved , relation_instance = utils .get_relation_instance (
287
- resource_instance , source , field .parent
288
- )
289
- if not resolved :
290
- continue
291
-
292
- if relation_instance is not None :
293
- relation_instance_id = relation_instance .pk
294
-
295
- data .update ({
296
- field_name : {
297
- 'data' : (
298
- OrderedDict ([
299
- ('type' , relation_type ),
300
- ('id' , encoding .force_str (relation_instance_id ))
301
- ]) if resource .get (field_name ) else None )
302
- }
303
- })
304
- continue
298
+ data .update ({
299
+ field_name : {
300
+ 'data' : (
301
+ OrderedDict ([
302
+ ('type' , relation_type ),
303
+ ('id' , encoding .force_str (relation_instance_id ))
304
+ ]) if resource .get (field_name ) else None )
305
+ }
306
+ })
307
+ continue
305
308
306
309
return utils .format_field_names (data )
307
310
@@ -344,12 +347,16 @@ def extract_included(cls, fields, resource, resource_instance, included_resource
344
347
if field_name == api_settings .URL_FIELD_NAME :
345
348
continue
346
349
347
- # Skip fields without relations or serialized data
350
+ # Skip fields without relations
348
351
if not isinstance (
349
- field , (relations .RelatedField , relations .ManyRelatedField , BaseSerializer )
352
+ field , (relations .RelatedField , relations .ManyRelatedField )
350
353
):
351
354
continue
352
355
356
+ if isinstance (field , BaseSerializer ) and \
357
+ rendering_strategy == ATTRIBUTE_RENDERING_STRATEGY :
358
+ continue
359
+
353
360
try :
354
361
included_resources .remove (field_name )
355
362
except ValueError :
@@ -392,66 +399,65 @@ def extract_included(cls, fields, resource, resource_instance, included_resource
392
399
for key in included_resources
393
400
if field_name == key .split ('.' )[0 ]]
394
401
395
- if rendering_strategy == RELATIONS_RENDERING_STRATEGY :
396
- if isinstance (field , ListSerializer ):
397
- serializer = field .child
398
- relation_type = utils .get_resource_type_from_serializer (serializer )
399
- relation_queryset = list (relation_instance )
400
-
401
- if serializer_data :
402
- for position in range (len (serializer_data )):
403
- serializer_resource = serializer_data [position ]
404
- nested_resource_instance = relation_queryset [position ]
405
- resource_type = (
406
- relation_type or
407
- utils .get_resource_type_from_instance (nested_resource_instance )
408
- )
409
- serializer_fields = utils .get_serializer_fields (
410
- serializer .__class__ (
411
- nested_resource_instance , context = serializer .context
412
- )
413
- )
414
- new_item = cls .build_json_resource_obj (
415
- serializer_fields ,
416
- serializer_resource ,
417
- nested_resource_instance ,
418
- resource_type ,
419
- getattr (serializer , '_poly_force_type_resolution' , False )
420
- )
421
- included_cache [new_item ['type' ]][new_item ['id' ]] = \
422
- utils .format_field_names (new_item )
423
- cls .extract_included (
424
- serializer_fields ,
425
- serializer_resource ,
426
- nested_resource_instance ,
427
- new_included_resources ,
428
- included_cache ,
402
+ if isinstance (field , ListSerializer ):
403
+ serializer = field .child
404
+ relation_type = utils .get_resource_type_from_serializer (serializer )
405
+ relation_queryset = list (relation_instance )
406
+
407
+ if serializer_data :
408
+ for position in range (len (serializer_data )):
409
+ serializer_resource = serializer_data [position ]
410
+ nested_resource_instance = relation_queryset [position ]
411
+ resource_type = (
412
+ relation_type or
413
+ utils .get_resource_type_from_instance (nested_resource_instance )
414
+ )
415
+ serializer_fields = utils .get_serializer_fields (
416
+ serializer .__class__ (
417
+ nested_resource_instance , context = serializer .context
429
418
)
430
-
431
- if isinstance (field , Serializer ):
432
- relation_type = utils .get_resource_type_from_serializer (field )
433
-
434
- # Get the serializer fields
435
- serializer_fields = utils .get_serializer_fields (field )
436
- if serializer_data :
419
+ )
437
420
new_item = cls .build_json_resource_obj (
438
421
serializer_fields ,
439
- serializer_data ,
440
- relation_instance ,
441
- relation_type ,
442
- getattr (field , '_poly_force_type_resolution' , False )
443
- )
444
- included_cache [new_item ['type' ]][new_item ['id' ]] = utils .format_field_names (
445
- new_item
422
+ serializer_resource ,
423
+ nested_resource_instance ,
424
+ resource_type ,
425
+ getattr (serializer , '_poly_force_type_resolution' , False )
446
426
)
427
+ included_cache [new_item ['type' ]][new_item ['id' ]] = \
428
+ utils .format_field_names (new_item )
447
429
cls .extract_included (
448
430
serializer_fields ,
449
- serializer_data ,
450
- relation_instance ,
431
+ serializer_resource ,
432
+ nested_resource_instance ,
451
433
new_included_resources ,
452
434
included_cache ,
453
435
)
454
436
437
+ if isinstance (field , Serializer ):
438
+ relation_type = utils .get_resource_type_from_serializer (field )
439
+
440
+ # Get the serializer fields
441
+ serializer_fields = utils .get_serializer_fields (field )
442
+ if serializer_data :
443
+ new_item = cls .build_json_resource_obj (
444
+ serializer_fields ,
445
+ serializer_data ,
446
+ relation_instance ,
447
+ relation_type ,
448
+ getattr (field , '_poly_force_type_resolution' , False )
449
+ )
450
+ included_cache [new_item ['type' ]][new_item ['id' ]] = utils .format_field_names (
451
+ new_item
452
+ )
453
+ cls .extract_included (
454
+ serializer_fields ,
455
+ serializer_data ,
456
+ relation_instance ,
457
+ new_included_resources ,
458
+ included_cache ,
459
+ )
460
+
455
461
@classmethod
456
462
def extract_meta (cls , serializer , resource ):
457
463
"""
0 commit comments