From 9153b62ea0e931f28f9bc6ce821b639d8a7617c3 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 27 Jan 2022 10:49:30 +1100 Subject: [PATCH] Handle case when aggregation returns None --- InvenTree/stock/models.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 8a68eb5940..158d0a2640 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -788,7 +788,12 @@ class StockItem(MPTTModel): query = self.allocations.aggregate(q=Coalesce(Sum('quantity'), Decimal(0))) - return query['q'] + total = query['q'] + + if total is None: + total = Decimal(0) + + return total def sales_order_allocation_count(self): """ @@ -797,14 +802,22 @@ class StockItem(MPTTModel): query = self.sales_order_allocations.aggregate(q=Coalesce(Sum('quantity'), Decimal(0))) - return query['q'] + total = query['q'] + + if total is None: + total = Decimal(0) + + return total def allocation_count(self): """ Return the total quantity allocated to builds or orders """ - return self.build_allocation_count() + self.sales_order_allocation_count() + bo = self.build_allocation_count() + so = self.sales_order_allocation_count() + + return bo + so def unallocated_quantity(self): """