@@ -199,15 +199,20 @@ def extract_relationships(fields, resource, resource_instance):
199199 continue
200200
201201 relation_type = get_related_resource_type (field )
202+ relation_instance_or_manager = getattr (resource_instance , field_name )
202203
203204 if isinstance (field , HyperlinkedIdentityField ):
204205 # special case for HyperlinkedIdentityField
205206 relation_data = list ()
206- relation_manager = getattr ( resource_instance , field_name )
207+
207208 # 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+ )
211216
212217 data .update ({field_name : {
213218 'links' : {
@@ -220,26 +225,26 @@ def extract_relationships(fields, resource, resource_instance):
220225 continue
221226
222227 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
224229
225230 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 )
229234 }
230235
231236 relation_data .update (
232237 {'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 ()
234239 )
235240 data .update ({field_name : relation_data })
236241 continue
237242
238243 if isinstance (field , ManyRelatedField ):
239244 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 ():
243248 relation_data .append (OrderedDict ([
244249 ('type' , relation_type ),
245250 ('id' , encoding .force_text (related_object .pk ))
@@ -261,14 +266,15 @@ def extract_relationships(fields, resource, resource_instance):
261266 relation_type = inflection .pluralize (relation_model .__name__ ).lower ()
262267
263268 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 ()
265270 if isinstance (serializer_data , list ):
266271 for position in range (len (serializer_data )):
267272 nested_resource_instance = resource_instance_queryset [position ]
268273 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+ )
272278
273279 data .update ({field_name : {'data' : relation_data }})
274280 continue
@@ -282,7 +288,7 @@ def extract_relationships(fields, resource, resource_instance):
282288 'data' : (
283289 OrderedDict ([
284290 ('type' , relation_type ),
285- ('id' , encoding .force_text (getattr ( resource_instance , field_name ) .pk ))
291+ ('id' , encoding .force_text (relation_instance_or_manager .pk ))
286292 ]) if resource .get (field_name ) else None )
287293 }
288294 })
@@ -302,38 +308,36 @@ def extract_included(fields, resource, resource_instance):
302308 if not isinstance (field , BaseSerializer ):
303309 continue
304310
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 )
306314
315+ if isinstance (field , ListSerializer ):
307316 serializer = field .child
308317 model = serializer .Meta .model
309318 relation_type = inflection .pluralize (model .__name__ ).lower ()
310319
311320 # Get the serializer fields
312321 serializer_fields = get_serializer_fields (serializer )
313- serializer_data = resource .get (field_name )
314- if isinstance (serializer_data , list ):
322+ if serializer_data :
315323 for position in range (len (serializer_data )):
316324 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 ]
319326 included_data .append (
320327 build_json_resource_obj (
321328 serializer_fields , serializer_resource , nested_resource_instance , relation_type
322329 )
323330 )
324331
325332 if isinstance (field , ModelSerializer ):
326-
327333 model = field .Meta .model
328334 relation_type = inflection .pluralize (model .__name__ ).lower ()
329335
330336 # Get the serializer fields
331337 serializer_fields = get_serializer_fields (field )
332- serializer_data = resource .get (field_name )
333- nested_resource_instance = getattr (resource_instance , field_name ).all ()
334338 if serializer_data :
335339 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 )
337341 )
338342
339343 return format_keys (included_data )
0 commit comments