|
13 | 13 |
|
14 | 14 | from django.utils.six.moves.urllib.parse import urlparse
|
15 | 15 |
|
16 |
| - |
17 | 16 | try:
|
18 | 17 | from rest_framework.compat import OrderedDict
|
19 | 18 | except ImportError:
|
@@ -234,46 +233,39 @@ def extract_relationships(fields, resource, resource_instance):
|
234 | 233 |
|
235 | 234 | if isinstance(field, (PrimaryKeyRelatedField, HyperlinkedRelatedField)):
|
236 | 235 | relation_type = get_related_resource_type(field)
|
| 236 | + relation_id = getattr(resource_instance, field_name).pk if resource.get(field_name) else None |
237 | 237 |
|
238 |
| - if resource.get(field_name) is not None: |
239 |
| - if isinstance(field, PrimaryKeyRelatedField): |
240 |
| - relation_id = encoding.force_text(resource.get(field_name)) |
241 |
| - elif isinstance(field, HyperlinkedRelatedField): |
242 |
| - relation_id = extract_id_from_url(resource.get(field_name)) |
243 |
| - else: |
244 |
| - relation_id = None |
245 |
| - |
246 |
| - data.update( |
247 |
| - { |
248 |
| - field_name: { |
249 |
| - 'data': (OrderedDict([ |
250 |
| - ('type', relation_type), ('id', relation_id) |
251 |
| - ]) if relation_id is not None else None) |
252 |
| - } |
253 |
| - } |
| 238 | + relation_data = { |
| 239 | + 'data': (OrderedDict([ |
| 240 | + ('type', relation_type), ('id', relation_id) |
| 241 | + ]) if relation_id is not None else None) |
| 242 | + } |
| 243 | + |
| 244 | + relation_data.update( |
| 245 | + {'links': {'related': resource.get(field_name)}} |
| 246 | + if isinstance(field, HyperlinkedRelatedField) and resource.get(field_name) else {} |
254 | 247 | )
|
| 248 | + data.update({field_name: relation_data}) |
255 | 249 | continue
|
256 | 250 |
|
257 | 251 | if isinstance(field, ManyRelatedField):
|
258 | 252 | relation_data = list()
|
259 |
| - |
260 | 253 | relation = field.child_relation
|
261 |
| - |
262 | 254 | relation_type = get_related_resource_type(relation)
|
263 |
| - |
264 |
| - if isinstance(relation, HyperlinkedRelatedField): |
265 |
| - for link in resource.get(field_name, list()): |
266 |
| - relation_data.append(OrderedDict([('type', relation_type), ('id', extract_id_from_url(link))])) |
267 |
| - |
268 |
| - data.update({field_name: {'data': relation_data}}) |
269 |
| - continue |
270 |
| - |
271 |
| - if isinstance(relation, PrimaryKeyRelatedField): |
272 |
| - for pk in resource.get(field_name, list()): |
273 |
| - relation_data.append(OrderedDict([('type', relation_type), ('id', encoding.force_text(pk))])) |
274 |
| - |
275 |
| - data.update({field_name: {'data': relation_data}}) |
276 |
| - continue |
| 255 | + for related_object in getattr(resource_instance, field_name).all(): |
| 256 | + relation_data.append(OrderedDict([ |
| 257 | + ('type', relation_type), |
| 258 | + ('id', encoding.force_text(related_object.pk)) |
| 259 | + ])) |
| 260 | + data.update({ |
| 261 | + field_name: { |
| 262 | + 'data': relation_data, |
| 263 | + 'meta': { |
| 264 | + 'count': len(relation_data) |
| 265 | + } |
| 266 | + } |
| 267 | + }) |
| 268 | + continue |
277 | 269 |
|
278 | 270 | if isinstance(field, ListSerializer):
|
279 | 271 | relation_data = list()
|
|
0 commit comments