diff --git a/InvenTree/build/api.py b/InvenTree/build/api.py index b987328702..8e0a92917e 100644 --- a/InvenTree/build/api.py +++ b/InvenTree/build/api.py @@ -36,7 +36,7 @@ class BuildList(generics.ListCreateAPIView): ] filter_fields = [ - 'build', + 'part', ] @@ -47,16 +47,33 @@ class BuildItemList(generics.ListCreateAPIView): - POST: Create a new BuildItem object """ - queryset = BuildItem.objects.all() serializer_class = BuildItemSerializer + def get_queryset(self): + """ Override the queryset method, + to allow filtering by stock_item.part + """ + + # Does the user wish to filter by part? + part_pk = self.request.query_params.get('part', None) + + query = BuildItem.objects.all() + + if part_pk: + query = query.filter(stock_item__part=part_pk) + + return query + permission_classes = [ permissions.IsAuthenticatedOrReadOnly, ] + filter_backends = [ + DjangoFilterBackend, + ] + filter_fields = [ 'build', - 'part', 'stock_item' ] diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py index 65fafc12ab..c5ef3e49eb 100644 --- a/InvenTree/build/serializers.py +++ b/InvenTree/build/serializers.py @@ -34,13 +34,16 @@ class BuildSerializer(InvenTreeModelSerializer): class BuildItemSerializer(InvenTreeModelSerializer): """ Serializes a BuildItem object """ - part = serializers.CharField(source='stock_item.part', read_only=True) + part = serializers.IntegerField(source='stock_item.part.pk', read_only=True) + part_name = serializers.CharField(source='stock_item.part', read_only=True) class Meta: model = BuildItem fields = [ + 'pk', 'build', 'part', + 'part_name', 'stock_item', 'quantity' ]