|
13 | 13 |
|
14 | 14 | from django.utils.six.moves.urllib.parse import urlparse |
15 | 15 |
|
| 16 | + |
16 | 17 | try: |
17 | 18 | from rest_framework.compat import OrderedDict |
18 | 19 | except ImportError: |
@@ -165,6 +166,18 @@ def get_related_resource_type(relation): |
165 | 166 | return inflection.pluralize(relation_model.__name__).lower() |
166 | 167 |
|
167 | 168 |
|
| 169 | +def get_current_instance(fields, resource): |
| 170 | + serializer_instance = fields.serializer.instance |
| 171 | + if isinstance(serializer_instance, list): |
| 172 | + current_id = extract_id(fields, resource) |
| 173 | + for instance in serializer_instance: |
| 174 | + # Search for our own instance |
| 175 | + if encoding.force_text(instance.pk) == current_id: |
| 176 | + return instance |
| 177 | + else: |
| 178 | + return serializer_instance |
| 179 | + |
| 180 | + |
168 | 181 | def extract_id_from_url(url): |
169 | 182 | http_prefix = url.startswith(('http:', 'https:')) |
170 | 183 | if http_prefix: |
@@ -217,8 +230,7 @@ def extract_relationships(fields, resource): |
217 | 230 | # special case for HyperlinkedRouterField |
218 | 231 | relation_data = list() |
219 | 232 | relation_type = get_related_resource_type(field) |
220 | | - parent_instance = field.parent.instance |
221 | | - related = getattr(parent_instance, field_name).all() if hasattr(parent_instance, field_name) else list() |
| 233 | + related = getattr(get_current_instance(fields, resource), field_name).all() |
222 | 234 | for relation in related: |
223 | 235 | relation_data.append(OrderedDict([('type', relation_type), ('id', relation.pk)])) |
224 | 236 |
|
|
0 commit comments