mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Annotate "in_stock" quantity to SupplierPart API (#3335)
* Annotate "in_stock" quantity to SupplierPart API * Increment API version
This commit is contained in:
		| @@ -263,6 +263,13 @@ class SupplierPartList(ListCreateDestroyAPIView): | ||||
|  | ||||
|     queryset = SupplierPart.objects.all() | ||||
|  | ||||
|     def get_queryset(self, *args, **kwargs): | ||||
|         """Return annotated queryest object for the SupplierPart list""" | ||||
|         queryset = super().get_queryset(*args, **kwargs) | ||||
|         queryset = SupplierPartSerializer.annotate_queryset(queryset) | ||||
|  | ||||
|         return queryset | ||||
|  | ||||
|     def filter_queryset(self, queryset): | ||||
|         """Custom filtering for the queryset.""" | ||||
|         queryset = super().filter_queryset(queryset) | ||||
|   | ||||
| @@ -5,6 +5,7 @@ from django.utils.translation import gettext_lazy as _ | ||||
| from rest_framework import serializers | ||||
| from sql_util.utils import SubqueryCount | ||||
|  | ||||
| import part.filters | ||||
| from common.settings import currency_code_default, currency_code_mappings | ||||
| from InvenTree.serializers import (InvenTreeAttachmentSerializer, | ||||
|                                    InvenTreeDecimalField, | ||||
| @@ -199,6 +200,9 @@ class ManufacturerPartParameterSerializer(InvenTreeModelSerializer): | ||||
| class SupplierPartSerializer(InvenTreeModelSerializer): | ||||
|     """Serializer for SupplierPart object.""" | ||||
|  | ||||
|     # Annotated field showing total in-stock quantity | ||||
|     in_stock = serializers.FloatField(read_only=True) | ||||
|  | ||||
|     part_detail = PartBriefSerializer(source='part', many=False, read_only=True) | ||||
|  | ||||
|     supplier_detail = CompanyBriefSerializer(source='supplier', many=False, read_only=True) | ||||
| @@ -249,6 +253,7 @@ class SupplierPartSerializer(InvenTreeModelSerializer): | ||||
|             'available', | ||||
|             'availability_updated', | ||||
|             'description', | ||||
|             'in_stock', | ||||
|             'link', | ||||
|             'manufacturer', | ||||
|             'manufacturer_detail', | ||||
| @@ -270,6 +275,20 @@ class SupplierPartSerializer(InvenTreeModelSerializer): | ||||
|             'availability_updated', | ||||
|         ] | ||||
|  | ||||
|     @staticmethod | ||||
|     def annotate_queryset(queryset): | ||||
|         """Annotate the SupplierPart queryset with extra fields: | ||||
|  | ||||
|         Fields: | ||||
|             in_stock: Current stock quantity for each SupplierPart | ||||
|         """ | ||||
|  | ||||
|         queryset = queryset.annotate( | ||||
|             in_stock=part.filters.annotate_total_stock(reference='part__') | ||||
|         ) | ||||
|  | ||||
|         return queryset | ||||
|  | ||||
|     def update(self, supplier_part, data): | ||||
|         """Custom update functionality for the serializer""" | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user