From 73d463d84ebbedabe4bdc76733339f11b61d3357 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 25 Jun 2025 18:34:57 +1000 Subject: [PATCH] Adjust "child_items" annotation for stockitem (#9842) * Adjust "child_items" annotation for stockitem - Show the direct children only * Remove unused func --- src/backend/InvenTree/stock/filters.py | 22 ---------------------- src/backend/InvenTree/stock/serializers.py | 2 +- 2 files changed, 1 insertion(+), 23 deletions(-) diff --git a/src/backend/InvenTree/stock/filters.py b/src/backend/InvenTree/stock/filters.py index ac3098a632..1e6900f36f 100644 --- a/src/backend/InvenTree/stock/filters.py +++ b/src/backend/InvenTree/stock/filters.py @@ -37,28 +37,6 @@ def annotate_location_items(filter: Q = None): ) -def annotate_child_items(): - """Construct a queryset annotation which returns the number of children below a certain StockItem node in a StockItem tree.""" - child_stock_query = stock.models.StockItem.objects.filter( - tree_id=OuterRef('tree_id'), - lft__gt=OuterRef('lft'), - rght__lt=OuterRef('rght'), - level__gte=OuterRef('level'), - ) - - return Coalesce( - Subquery( - child_stock_query.annotate( - count=Func(F('pk'), function='COUNT', output_field=IntegerField()) - ) - .values('count') - .order_by() - ), - 0, - output_field=IntegerField(), - ) - - def annotate_sub_locations(): """Construct a queryset annotation which returns the number of sub-locations below a certain StockLocation node in a StockLocation tree.""" subquery = stock.models.StockLocation.objects.filter( diff --git a/src/backend/InvenTree/stock/serializers.py b/src/backend/InvenTree/stock/serializers.py index 89eaf4bf4a..a59895ebbf 100644 --- a/src/backend/InvenTree/stock/serializers.py +++ b/src/backend/InvenTree/stock/serializers.py @@ -604,7 +604,7 @@ class StockItemSerializer( queryset = queryset.annotate(installed_items=SubqueryCount('installed_parts')) # Annotate with the total number of "child items" (split stock items) - queryset = queryset.annotate(child_items=stock.filters.annotate_child_items()) + queryset = queryset.annotate(child_items=SubqueryCount('children')) return queryset