From cf6a03456f12157c38521f4d7a95f79730538f5f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 22 Feb 2021 18:44:31 +1100 Subject: [PATCH] Adds 'suppliers' field to Part serializer - Number of supplier parts associated with the Part object --- InvenTree/part/serializers.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 103d0202f1..1ada816357 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -245,6 +245,14 @@ class PartSerializer(InvenTreeModelSerializer): Decimal(0), ) ) + + # Annotate with the number of 'suppliers' + queryset = queryset.annotate( + suppliers=Coalesce( + SubqueryCount('supplier_parts'), + Decimal(0), + ), + ) return queryset @@ -263,6 +271,7 @@ class PartSerializer(InvenTreeModelSerializer): ordering = serializers.FloatField(read_only=True) building = serializers.FloatField(read_only=True) stock_item_count = serializers.IntegerField(read_only=True) + suppliers = serializers.IntegerField(read_only=True) image = serializers.CharField(source='get_image_url', read_only=True) thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True) @@ -308,6 +317,7 @@ class PartSerializer(InvenTreeModelSerializer): 'salable', 'starred', 'stock_item_count', + 'suppliers', 'thumbnail', 'trackable', 'units',