diff --git a/InvenTree/company/api.py b/InvenTree/company/api.py index 19ae54af32..b8d4531ef0 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, @@ -67,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 780492cdd5..09f2b4dee8 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(read_only=True) + parts_manufactured = serializers.IntegerField(read_only=True) + class Meta: model = Company fields = [ @@ -57,7 +69,8 @@ class CompanySerializer(InvenTreeModelSerializer): 'is_manufacturer', 'is_supplier', 'notes', - 'part_count' + 'parts_supplied', + 'parts_manufactured', ]