@@ -166,19 +166,6 @@ def get_related_resource_type(relation):
166
166
return inflection .pluralize (relation_model .__name__ ).lower ()
167
167
168
168
169
- def extract_id_from_url (url ):
170
- http_prefix = url .startswith (('http:' , 'https:' ))
171
- if http_prefix :
172
- # If needed convert absolute URLs to relative path
173
- data = urlparse (url ).path
174
- prefix = urlresolvers .get_script_prefix ()
175
- if data .startswith (prefix ):
176
- url = '/' + data [len (prefix ):]
177
-
178
- match = urlresolvers .resolve (url )
179
- return encoding .force_text (match .kwargs ['pk' ])
180
-
181
-
182
169
def extract_attributes (fields , resource ):
183
170
data = OrderedDict ()
184
171
for field_name , field in six .iteritems (fields ):
@@ -228,10 +215,7 @@ def extract_relationships(fields, resource, resource_instance):
228
215
relation_type = get_related_resource_type (field )
229
216
230
217
if resource .get (field_name ) is not None :
231
- if isinstance (field , PrimaryKeyRelatedField ):
232
- relation_id = encoding .force_text (resource .get (field_name ))
233
- elif isinstance (field , HyperlinkedRelatedField ):
234
- relation_id = extract_id_from_url (resource .get (field_name ))
218
+ relation_id = getattr (resource_instance , field_name ).id
235
219
else :
236
220
relation_id = None
237
221
@@ -250,19 +234,14 @@ def extract_relationships(fields, resource, resource_instance):
250
234
relation_data = list ()
251
235
252
236
relation = field .child_relation
253
-
254
237
relation_type = get_related_resource_type (relation )
255
-
256
- if isinstance (relation , HyperlinkedRelatedField ):
257
- for link in resource .get (field_name , list ()):
258
- relation_data .append (OrderedDict ([('type' , relation_type ), ('id' , extract_id_from_url (link ))]))
259
-
260
- data .update ({field_name : {'data' : relation_data }})
261
- continue
262
-
263
- if isinstance (relation , PrimaryKeyRelatedField ):
264
- for pk in resource .get (field_name , list ()):
265
- relation_data .append (OrderedDict ([('type' , relation_type ), ('id' , encoding .force_text (pk ))]))
238
+ nested_resource_queryset = getattr (resource_instance , field_name ).all ()
239
+ if isinstance (relation , (HyperlinkedRelatedField , PrimaryKeyRelatedField )):
240
+ for position in range (len (nested_resource_queryset )):
241
+ nested_resource_instance = nested_resource_queryset [position ]
242
+ relation_data .append (
243
+ OrderedDict ([('type' , relation_type ), ('id' , encoding .force_text (nested_resource_instance .pk ))])
244
+ )
266
245
267
246
data .update ({field_name : {'data' : relation_data }})
268
247
continue
@@ -275,10 +254,10 @@ def extract_relationships(fields, resource, resource_instance):
275
254
relation_type = inflection .pluralize (relation_model .__name__ ).lower ()
276
255
277
256
serializer_data = resource .get (field_name )
257
+ resource_instance_queryset = getattr (resource_instance , field_name ).all ()
278
258
if isinstance (serializer_data , list ):
279
259
for position in range (len (serializer_data )):
280
- resource_instance_manager = getattr (resource_instance , field_name ).all ()
281
- nested_resource_instance = resource_instance_manager [position ]
260
+ nested_resource_instance = resource_instance_queryset [position ]
282
261
relation_data .append (
283
262
OrderedDict ([
284
263
('type' , relation_type ), ('id' , encoding .force_text (nested_resource_instance .pk ))
0 commit comments