From 9b4e443289e45ea0e8100b42938145bfc3b18b9d Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 22 Jun 2022 20:50:21 +1000 Subject: [PATCH] Prevent calculation of 'allocation_count' before model is saved (#3235) --- InvenTree/part/models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 4c02ee34f6..38319a2e91 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1318,6 +1318,11 @@ class Part(MetadataMixin, MPTTModel): def allocation_count(self, **kwargs): """Return the total quantity of stock allocated for this part, against both build orders and sales orders.""" + + if self.id is None: + # If this instance has not been saved, foreign-key lookups will fail + return 0 + return sum( [ self.build_order_allocation_count(**kwargs),