better prefetching

This commit is contained in:
Oliver Walters committed 2026-11-12 11:43:11 +00:00
1 parent 8911c22e35
commit d14269d465
1 file changed
+11 -4
+11 -4
View File
@@ -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(