From 72aba30e8190a43a81f28354e630ddf573a313e3 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 21 May 2019 00:16:00 +1000 Subject: [PATCH] More hungry fetching --- InvenTree/build/models.py | 6 +++--- InvenTree/part/models.py | 2 +- InvenTree/part/serializers.py | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index c69857fda3..92f04bdcc6 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -139,7 +139,7 @@ class Build(models.Model): allocations = [] - for item in self.part.bom_items.all(): + for item in self.part.bom_items.all().prefetch_related('sub_part'): # How many parts required for this build? q_required = item.quantity * self.quantity @@ -216,7 +216,7 @@ class Build(models.Model): - Delete pending BuildItem objects """ - for item in self.allocated_stock.all(): + for item in self.allocated_stock.all().prefetch_related('stock_item'): # Subtract stock from the item item.stock_item.take_stock( @@ -295,7 +295,7 @@ class Build(models.Model): """ Returns a dict of parts required to build this part (BOM) """ parts = [] - for item in self.part.bom_items.all(): + for item in self.part.bom_items.all().prefetch_related('sub_part'): part = {'part': item.sub_part, 'per_build': item.quantity, 'quantity': item.quantity * self.quantity, diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 6f1563ef8b..46afc5d1f1 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -532,7 +532,7 @@ class Part(models.Model): hash = hashlib.md5(str(self.id).encode()) - for item in self.bom_items.all().prefetch('sub_part'): + for item in self.bom_items.all().prefetch_related('sub_part'): hash.update(str(item.sub_part.id).encode()) hash.update(str(item.sub_part.full_name).encode()) hash.update(str(item.quantity).encode()) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 6566ba52ad..ddbbab9e57 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -125,6 +125,9 @@ class BomItemSerializer(InvenTreeModelSerializer): @staticmethod def setup_eager_loading(queryset): + queryset = queryset.prefetch_related('part') + queryset = queryset.prefetch_related('part__category') + queryset = queryset.prefetch_related('part__stock_items') queryset = queryset.prefetch_related('sub_part') queryset = queryset.prefetch_related('sub_part__category') queryset = queryset.prefetch_related('sub_part__stock_items')