Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Reduce the number of queries

Patryk Zawadzki5aeb1f4b0e78
diff --git a/saleor/warehouse/availability.py b/saleor/warehouse/availability.py
index c87b5bc995..0c632663e9 100644
--- a/saleor/warehouse/availability.py
+++ b/saleor/warehouse/availability.py
@@ -18,10 +18,12 @@ def _get_quantity_allocated(stocks: StockQuerySet) -> int:
def _get_available_quantity(stocks: StockQuerySet) -> int:
- quantity_allocated = _get_quantity_allocated(stocks)
- total_quantity = stocks.aggregate(total_quantity=Coalesce(Sum("quantity"), 0))[
- "total_quantity"
- ]
+ results = stocks.aggregate(
+ total_quantity=Coalesce(Sum("quantity"), 0),
+ quantity_allocated=Coalesce(Sum("allocations__quantity_allocated"), 0),
+ )
+ total_quantity = results["total_quantity"]
+ quantity_allocated = results["quantity_allocated"]
return max(total_quantity - quantity_allocated, 0)