@@ -199,15 +199,20 @@ def extract_relationships(fields, resource, resource_instance):
199
199
continue
200
200
201
201
relation_type = get_related_resource_type (field )
202
+ relation_instance_or_manager = getattr (resource_instance , field_name )
202
203
203
204
if isinstance (field , HyperlinkedIdentityField ):
204
205
# special case for HyperlinkedIdentityField
205
206
relation_data = list ()
206
- relation_manager = getattr ( resource_instance , field_name )
207
+
207
208
# Don't try to query an empty relation
208
- related = relation_manager .all () if relation_manager is not None else list ()
209
- for relation in related :
210
- relation_data .append (OrderedDict ([('type' , relation_type ), ('id' , encoding .force_text (relation .pk ))]))
209
+ relation_queryset = relation_instance_or_manager .all () \
210
+ if relation_instance_or_manager is not None else list ()
211
+
212
+ for related_object in relation_queryset :
213
+ relation_data .append (
214
+ OrderedDict ([('type' , relation_type ), ('id' , encoding .force_text (related_object .pk ))])
215
+ )
211
216
212
217
data .update ({field_name : {
213
218
'links' : {
@@ -220,26 +225,26 @@ def extract_relationships(fields, resource, resource_instance):
220
225
continue
221
226
222
227
if isinstance (field , (PrimaryKeyRelatedField , HyperlinkedRelatedField )):
223
- relation_id = getattr ( resource_instance , field_name ) .pk if resource .get (field_name ) else None
228
+ relation_id = relation_instance_or_manager .pk if resource .get (field_name ) else None
224
229
225
230
relation_data = {
226
- 'data' : (OrderedDict ([
227
- ( 'type' , relation_type ), ('id' , encoding .force_text (relation_id ))
228
- ]) if relation_id is not None else None )
231
+ 'data' : (
232
+ OrderedDict ([( 'type' , relation_type ), ('id' , encoding .force_text (relation_id ))] )
233
+ if relation_id is not None else None )
229
234
}
230
235
231
236
relation_data .update (
232
237
{'links' : {'related' : resource .get (field_name )}}
233
- if isinstance (field , HyperlinkedRelatedField ) and resource .get (field_name ) else {}
238
+ if isinstance (field , HyperlinkedRelatedField ) and resource .get (field_name ) else dict ()
234
239
)
235
240
data .update ({field_name : relation_data })
236
241
continue
237
242
238
243
if isinstance (field , ManyRelatedField ):
239
244
relation_data = list ()
240
- relation = field .child_relation
241
- relation_type = get_related_resource_type (relation )
242
- for related_object in getattr ( resource_instance , field_name ) .all ():
245
+ related_object = field .child_relation
246
+ relation_type = get_related_resource_type (related_object )
247
+ for related_object in relation_instance_or_manager .all ():
243
248
relation_data .append (OrderedDict ([
244
249
('type' , relation_type ),
245
250
('id' , encoding .force_text (related_object .pk ))
@@ -261,14 +266,15 @@ def extract_relationships(fields, resource, resource_instance):
261
266
relation_type = inflection .pluralize (relation_model .__name__ ).lower ()
262
267
263
268
serializer_data = resource .get (field_name )
264
- resource_instance_queryset = getattr ( resource_instance , field_name ) .all ()
269
+ resource_instance_queryset = relation_instance_or_manager .all ()
265
270
if isinstance (serializer_data , list ):
266
271
for position in range (len (serializer_data )):
267
272
nested_resource_instance = resource_instance_queryset [position ]
268
273
relation_data .append (
269
- OrderedDict ([
270
- ('type' , relation_type ), ('id' , encoding .force_text (nested_resource_instance .pk ))
271
- ]))
274
+ OrderedDict (
275
+ [('type' , relation_type ), ('id' , encoding .force_text (nested_resource_instance .pk ))]
276
+ )
277
+ )
272
278
273
279
data .update ({field_name : {'data' : relation_data }})
274
280
continue
@@ -282,7 +288,7 @@ def extract_relationships(fields, resource, resource_instance):
282
288
'data' : (
283
289
OrderedDict ([
284
290
('type' , relation_type ),
285
- ('id' , encoding .force_text (getattr ( resource_instance , field_name ) .pk ))
291
+ ('id' , encoding .force_text (relation_instance_or_manager .pk ))
286
292
]) if resource .get (field_name ) else None )
287
293
}
288
294
})
@@ -302,38 +308,36 @@ def extract_included(fields, resource, resource_instance):
302
308
if not isinstance (field , BaseSerializer ):
303
309
continue
304
310
305
- if isinstance (field , ListSerializer ):
311
+ relation_instance_or_manager = getattr (resource_instance , field_name )
312
+ relation_queryset = relation_instance_or_manager .all ()
313
+ serializer_data = resource .get (field_name )
306
314
315
+ if isinstance (field , ListSerializer ):
307
316
serializer = field .child
308
317
model = serializer .Meta .model
309
318
relation_type = inflection .pluralize (model .__name__ ).lower ()
310
319
311
320
# Get the serializer fields
312
321
serializer_fields = get_serializer_fields (serializer )
313
- serializer_data = resource .get (field_name )
314
- if isinstance (serializer_data , list ):
322
+ if serializer_data :
315
323
for position in range (len (serializer_data )):
316
324
serializer_resource = serializer_data [position ]
317
- resource_instance_manager = getattr (resource_instance , field_name ).all ()
318
- nested_resource_instance = resource_instance_manager [position ]
325
+ nested_resource_instance = relation_queryset [position ]
319
326
included_data .append (
320
327
build_json_resource_obj (
321
328
serializer_fields , serializer_resource , nested_resource_instance , relation_type
322
329
)
323
330
)
324
331
325
332
if isinstance (field , ModelSerializer ):
326
-
327
333
model = field .Meta .model
328
334
relation_type = inflection .pluralize (model .__name__ ).lower ()
329
335
330
336
# Get the serializer fields
331
337
serializer_fields = get_serializer_fields (field )
332
- serializer_data = resource .get (field_name )
333
- nested_resource_instance = getattr (resource_instance , field_name ).all ()
334
338
if serializer_data :
335
339
included_data .append (
336
- build_json_resource_obj (serializer_fields , serializer_data , nested_resource_instance , relation_type )
340
+ build_json_resource_obj (serializer_fields , serializer_data , relation_queryset , relation_type )
337
341
)
338
342
339
343
return format_keys (included_data )
0 commit comments