From 9d5b938ceda62fef34181855f7167f966b00b285 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 21 May 2019 07:57:44 +1000 Subject: [PATCH 1/2] Don't serialize 'available_stock' in PartSerializer --- InvenTree/part/models.py | 11 ++++++++--- InvenTree/part/serializers.py | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 04277508c7..af23b7484b 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -17,6 +17,7 @@ from django.conf import settings from django.core.files.base import ContentFile from django.db import models, transaction from django.db.models import Sum +from django.db.models import prefetch_related_objects from django.core.validators import MinValueValidator from django.contrib.staticfiles.templatetags.staticfiles import static @@ -411,7 +412,7 @@ class Part(models.Model): total = None # Calculate the minimum number of parts that can be built using each sub-part - for item in self.bom_items.all().select_related('sub_part'): + for item in self.bom_items.all().prefetch_related('sub_part__stock_items'): stock = item.sub_part.available_stock n = int(1.0 * stock / item.quantity) @@ -449,9 +450,11 @@ class Part(models.Model): builds = [] - for item in self.used_in.all().prefetch_related('part'): + for item in self.used_in.all().prefetch_related('part__builds'): - for build in item.part.active_builds: + active = item.part.active_builds + + for build in active: b = {} b['build'] = build @@ -459,6 +462,8 @@ class Part(models.Model): builds.append(b) + prefetch_related_objects(builds, 'build_items') + return builds @property diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index ddbbab9e57..213cb9c75e 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -89,7 +89,7 @@ class PartSerializer(serializers.ModelSerializer): 'keywords', 'URL', 'total_stock', - 'available_stock', + # 'available_stock', 'units', 'trackable', 'buildable', From f19d4eec79b69dba6b23e9fe8088d5052ad4006c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 21 May 2019 08:19:16 +1000 Subject: [PATCH 2/2] Improve query speed for supplierpart api --- InvenTree/company/api.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 894974d3e9..f7bf9acc3f 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -75,7 +75,14 @@ class SupplierPartList(generics.ListCreateAPIView): - POST: Create a new SupplierPart object """ - queryset = SupplierPart.objects.all() + queryset = SupplierPart.objects.all().prefetch_related( + 'part', + 'part__category', + 'part__stock_items', + 'part__bom_items', + 'part__builds', + 'supplier') + serializer_class = SupplierPartSerializer permission_classes = [