mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Allow SupplierPart list to be filtered by SKU (#3417)
* Allow SupplierPart list to be filtered by SKU * Bump API version * Allow SupplierPart list to be filtered by MPN (manufacturer part number) * Simplify filter definition * Code formatting * Update API docs
This commit is contained in:
		| @@ -254,6 +254,31 @@ class ManufacturerPartParameterDetail(RetrieveUpdateDestroyAPI): | ||||
|     serializer_class = ManufacturerPartParameterSerializer | ||||
|  | ||||
|  | ||||
| class SupplierPartFilter(rest_filters.FilterSet): | ||||
|     """API filters for the SupplierPartList endpoint""" | ||||
|  | ||||
|     class Meta: | ||||
|         """Metaclass option""" | ||||
|  | ||||
|         model = SupplierPart | ||||
|         fields = [ | ||||
|             'supplier', | ||||
|             'part', | ||||
|             'manufacturer_part', | ||||
|             'SKU', | ||||
|         ] | ||||
|  | ||||
|     # Filter by 'active' status of linked part | ||||
|     active = rest_filters.BooleanFilter(field_name='part__active') | ||||
|  | ||||
|     # Filter by the 'MPN' of linked manufacturer part | ||||
|     MPN = rest_filters.CharFilter( | ||||
|         label='Manufacturer Part Number', | ||||
|         field_name='manufacturer_part__MPN', | ||||
|         lookup_expr='iexact' | ||||
|     ) | ||||
|  | ||||
|  | ||||
| class SupplierPartList(ListCreateDestroyAPIView): | ||||
|     """API endpoint for list view of SupplierPart object. | ||||
|  | ||||
| @@ -262,6 +287,7 @@ class SupplierPartList(ListCreateDestroyAPIView): | ||||
|     """ | ||||
|  | ||||
|     queryset = SupplierPart.objects.all() | ||||
|     filterset_class = SupplierPartFilter | ||||
|  | ||||
|     def get_queryset(self, *args, **kwargs): | ||||
|         """Return annotated queryest object for the SupplierPart list""" | ||||
| @@ -282,37 +308,12 @@ class SupplierPartList(ListCreateDestroyAPIView): | ||||
|         if manufacturer is not None: | ||||
|             queryset = queryset.filter(manufacturer_part__manufacturer=manufacturer) | ||||
|  | ||||
|         # Filter by supplier | ||||
|         supplier = params.get('supplier', None) | ||||
|  | ||||
|         if supplier is not None: | ||||
|             queryset = queryset.filter(supplier=supplier) | ||||
|  | ||||
|         # Filter by EITHER manufacturer or supplier | ||||
|         company = params.get('company', None) | ||||
|  | ||||
|         if company is not None: | ||||
|             queryset = queryset.filter(Q(manufacturer_part__manufacturer=company) | Q(supplier=company)) | ||||
|  | ||||
|         # Filter by parent part? | ||||
|         part = params.get('part', None) | ||||
|  | ||||
|         if part is not None: | ||||
|             queryset = queryset.filter(part=part) | ||||
|  | ||||
|         # Filter by manufacturer part? | ||||
|         manufacturer_part = params.get('manufacturer_part', None) | ||||
|  | ||||
|         if manufacturer_part is not None: | ||||
|             queryset = queryset.filter(manufacturer_part=manufacturer_part) | ||||
|  | ||||
|         # Filter by 'active' status of the part? | ||||
|         active = params.get('active', None) | ||||
|  | ||||
|         if active is not None: | ||||
|             active = str2bool(active) | ||||
|             queryset = queryset.filter(part__active=active) | ||||
|  | ||||
|         return queryset | ||||
|  | ||||
|     def get_serializer(self, *args, **kwargs): | ||||
|   | ||||
		Reference in New Issue
	
	Block a user