From a8e3a3f1ac211e52b815272efac7b990fd95ad50 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 2 May 2020 15:23:28 +1000 Subject: [PATCH 1/2] Reduce company list from 100 queries to 4 queries --- InvenTree/company/api.py | 7 +++++++ InvenTree/company/serializers.py | 19 ++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 19ae54af32..c081048e74 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -32,6 +32,13 @@ class CompanyList(generics.ListCreateAPIView): serializer_class = CompanySerializer queryset = Company.objects.all() + + def get_queryset(self): + + queryset = super().get_queryset() + queryset = CompanySerializer.annotate_queryset(queryset) + + return queryset permission_classes = [ permissions.IsAuthenticated, diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index 780492cdd5..7873e02cb3 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -4,6 +4,8 @@ JSON serializers for Company app from rest_framework import serializers +from django.db.models import Count + from .models import Company from .models import SupplierPart, SupplierPriceBreak @@ -33,11 +35,21 @@ class CompanyBriefSerializer(InvenTreeModelSerializer): class CompanySerializer(InvenTreeModelSerializer): """ Serializer for Company object (full detail) """ - url = serializers.CharField(source='get_absolute_url', read_only=True) - part_count = serializers.CharField(read_only=True) + @staticmethod + def annotate_queryset(queryset): + return queryset.annotate( + parts_supplied=Count('supplied_parts'), + parts_manufactured=Count('manufactured_parts') + ) + + url = serializers.CharField(source='get_absolute_url', read_only=True) + image = serializers.CharField(source='get_thumbnail_url', read_only=True) + parts_supplied = serializers.IntegerField() + parts_manufactured = serializers.IntegerField() + class Meta: model = Company fields = [ @@ -57,7 +69,8 @@ class CompanySerializer(InvenTreeModelSerializer): 'is_manufacturer', 'is_supplier', 'notes', - 'part_count' + 'parts_supplied', + 'parts_manufactured', ] From b0f4475a834890f77aec58c8c61aeee7707abdf6 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 2 May 2020 15:36:48 +1000 Subject: [PATCH 2/2] Fixes --- InvenTree/company/api.py | 7 +++++++ InvenTree/company/serializers.py | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index c081048e74..b8d4531ef0 100644 --- a/InvenTree/company/api.py +++ b/InvenTree/company/api.py @@ -74,6 +74,13 @@ class CompanyDetail(generics.RetrieveUpdateDestroyAPIView): queryset = Company.objects.all() serializer_class = CompanySerializer + + def get_queryset(self): + + queryset = super().get_queryset() + queryset = CompanySerializer.annotate_queryset(queryset) + + return queryset permission_classes = [ permissions.IsAuthenticated, diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index 7873e02cb3..09f2b4dee8 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -47,8 +47,8 @@ class CompanySerializer(InvenTreeModelSerializer): image = serializers.CharField(source='get_thumbnail_url', read_only=True) - parts_supplied = serializers.IntegerField() - parts_manufactured = serializers.IntegerField() + parts_supplied = serializers.IntegerField(read_only=True) + parts_manufactured = serializers.IntegerField(read_only=True) class Meta: model = Company