Adds 'suppliers' field to Part serializer

- Number of supplier parts associated with the Part object
This commit is contained in:
Oliver Walters 2021-02-22 18:44:31 +11:00
parent 5d1e6981d8
commit cf6a03456f

View File

@ -245,6 +245,14 @@ class PartSerializer(InvenTreeModelSerializer):
Decimal(0), Decimal(0),
) )
) )
# Annotate with the number of 'suppliers'
queryset = queryset.annotate(
suppliers=Coalesce(
SubqueryCount('supplier_parts'),
Decimal(0),
),
)
return queryset return queryset
@ -263,6 +271,7 @@ class PartSerializer(InvenTreeModelSerializer):
ordering = serializers.FloatField(read_only=True) ordering = serializers.FloatField(read_only=True)
building = serializers.FloatField(read_only=True) building = serializers.FloatField(read_only=True)
stock_item_count = serializers.IntegerField(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) image = serializers.CharField(source='get_image_url', read_only=True)
thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True) thumbnail = serializers.CharField(source='get_thumbnail_url', read_only=True)
@ -308,6 +317,7 @@ class PartSerializer(InvenTreeModelSerializer):
'salable', 'salable',
'starred', 'starred',
'stock_item_count', 'stock_item_count',
'suppliers',
'thumbnail', 'thumbnail',
'trackable', 'trackable',
'units', 'units',