diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index 335853331c..6f5327baa4 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -6,7 +6,7 @@ Provides a JSON API for the Part app from __future__ import unicode_literals from django_filters.rest_framework import DjangoFilterBackend - +from django.http import JsonResponse from django.db.models import Q, F, Count from rest_framework import status @@ -264,7 +264,15 @@ class PartList(generics.ListCreateAPIView): part['category_detail'] = detail - return Response(data) + """ + Determine the response type based on the request. + a) For HTTP requests (e.g. via the browseable API) return a DRF response + b) For AJAX requests, simply return a JSON rendered response. + """ + if request.is_ajax(): + return JsonResponse(data, safe=False) + else: + return Response(data) def create(self, request, *args, **kwargs): """ Override the default 'create' behaviour: @@ -288,6 +296,7 @@ class PartList(generics.ListCreateAPIView): queryset = super().get_queryset(*args, **kwargs) queryset = part_serializers.PartSerializer.prefetch_queryset(queryset) + queryset = part_serializers.PartSerializer.annotate_queryset(queryset) return queryset diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 86a3189c43..920e0486c3 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -130,12 +130,7 @@ class PartSerializer(InvenTreeModelSerializer): # Annotate the number total stock count queryset = queryset.annotate( - in_stock=Coalesce(Sum('stock_items__quantity', filter=stock_filter, distinct=True), Decimal(0)) - ) - - # Annotate the number of parts "on order" - # Total "on order" parts = "Quantity" - "Received" for each active purchase order - queryset = queryset.annotate( + in_stock=Coalesce(Sum('stock_items__quantity', filter=stock_filter, distinct=True), Decimal(0)), ordering=Coalesce(Sum( 'supplier_parts__purchase_order_line_items__quantity', filter=order_filter, @@ -144,11 +139,7 @@ class PartSerializer(InvenTreeModelSerializer): 'supplier_parts__purchase_order_line_items__received', filter=order_filter, distinct=True - ), Decimal(0)) - ) - - # Annotate number of parts being build - queryset = queryset.annotate( + ), Decimal(0)), building=Coalesce( Sum('builds__quantity', filter=build_filter, distinct=True), Decimal(0) )