Skip to content

Commit 2f49d37

Browse files
committed
Fixed issue where the field would not return the proper relations and count in list view
1 parent 24aaabd commit 2f49d37

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

rest_framework_json_api/utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from django.utils.six.moves.urllib.parse import urlparse
1515

16+
1617
try:
1718
from rest_framework.compat import OrderedDict
1819
except ImportError:
@@ -165,6 +166,18 @@ def get_related_resource_type(relation):
165166
return inflection.pluralize(relation_model.__name__).lower()
166167

167168

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+
168181
def extract_id_from_url(url):
169182
http_prefix = url.startswith(('http:', 'https:'))
170183
if http_prefix:
@@ -217,8 +230,7 @@ def extract_relationships(fields, resource):
217230
# special case for HyperlinkedRouterField
218231
relation_data = list()
219232
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()
222234
for relation in related:
223235
relation_data.append(OrderedDict([('type', relation_type), ('id', relation.pk)]))
224236

0 commit comments

Comments
 (0)