From e02b692ab2cbe47e80e14a1abbd6067ce1e9ccbe Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 16 May 2020 23:19:16 +1000 Subject: [PATCH] Improve API filtering for StockItem --- InvenTree/stock/api.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 2580f1d221..342504b99a 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -538,11 +538,10 @@ class StockList(generics.ListCreateAPIView): try: part = Part.objects.get(pk=part_id) - # If the part is a Template part, select stock items for any "variant" parts under that template - if part.is_template: - queryset = queryset.filter(part__in=[part.id for part in Part.objects.filter(variant_of=part_id)]) - else: - queryset = queryset.filter(part=part_id) + # Filter by any parts "under" the given part + parts = part.get_descendants(include_self=True) + + queryset = queryset.filter(part__in=parts) except (ValueError, Part.DoesNotExist): raise ValidationError({"part": "Invalid Part ID specified"})