diff --git a/src/backend/InvenTree/stock/serializers.py b/src/backend/InvenTree/stock/serializers.py index 77250e553a..d80176ead1 100644 --- a/src/backend/InvenTree/stock/serializers.py +++ b/src/backend/InvenTree/stock/serializers.py @@ -516,17 +516,25 @@ class StockItemSerializer( @staticmethod def annotate_queryset(queryset): """Add some extra annotations to the queryset, performing database queries as efficiently as possible.""" + # Note: 'part' must be loaded via Prefetch (not select_related), + # otherwise the select_related cache causes the Prefetch to be skipped, + # and the 'category_default_location' annotation is silently lost. + # Prefetching also loads each distinct Part (and PartPricing) only once, + # rather than once per StockItem row. queryset = queryset.prefetch_related( 'location', 'sales_order', 'purchase_order', Prefetch( 'part', - queryset=part_models.Part.objects.annotate( + queryset=part_models.Part.objects + .annotate( category_default_location=part_filters.annotate_default_location( 'category__' ) - ).prefetch_related(None), + ) + .prefetch_related(None) + .select_related('pricing_data'), ), 'parent', 'part__category', @@ -534,9 +542,8 @@ class StockItemSerializer( 'supplier_part__manufacturer_part', 'customer', 'belongs_to', - 'sales_order', 'consumed_by', - ).select_related('part', 'part__pricing_data') + ) # Annotate the queryset with the total allocated to sales orders queryset = queryset.annotate(