Override get_queryset for BuildItem API view

- Special management to allow filtering by part
This commit is contained in:
Oliver Walters 2019-04-29 23:45:05 +10:00
parent c29827e45a
commit 192f604b76
2 changed files with 24 additions and 4 deletions

View File

@ -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'
]

View File

@ -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'
]