Merge remote-tracking branch 'inventree/master'

This commit is contained in:
Oliver Walters 2020-05-02 17:30:08 +10:00
commit 937211288e
2 changed files with 30 additions and 3 deletions

View File

@ -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,

View File

@ -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',
]