@@ -166,19 +166,6 @@ def get_related_resource_type(relation):
166166 return inflection .pluralize (relation_model .__name__ ).lower ()
167167
168168
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-
182169def extract_attributes (fields , resource ):
183170 data = OrderedDict ()
184171 for field_name , field in six .iteritems (fields ):
@@ -228,10 +215,7 @@ def extract_relationships(fields, resource, resource_instance):
228215 relation_type = get_related_resource_type (field )
229216
230217 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
235219 else :
236220 relation_id = None
237221
@@ -250,19 +234,14 @@ def extract_relationships(fields, resource, resource_instance):
250234 relation_data = list ()
251235
252236 relation = field .child_relation
253-
254237 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+ )
266245
267246 data .update ({field_name : {'data' : relation_data }})
268247 continue
@@ -275,10 +254,10 @@ def extract_relationships(fields, resource, resource_instance):
275254 relation_type = inflection .pluralize (relation_model .__name__ ).lower ()
276255
277256 serializer_data = resource .get (field_name )
257+ resource_instance_queryset = getattr (resource_instance , field_name ).all ()
278258 if isinstance (serializer_data , list ):
279259 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 ]
282261 relation_data .append (
283262 OrderedDict ([
284263 ('type' , relation_type ), ('id' , encoding .force_text (nested_resource_instance .pk ))
0 commit comments