From fc75ab7420e0b0c16d08e478bcc0ab8fafeb6741 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Mon, 20 May 2019 08:44:52 +1000 Subject: [PATCH] Reduce response time for stock list Initial time - 6.2s After prefetch - 4.3s --- InvenTree/part/serializers.py | 1 + InvenTree/stock/api.py | 3 +++ InvenTree/stock/serializers.py | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/InvenTree/part/serializers.py b/InvenTree/part/serializers.py index 9251580bcc..d4eb82d0a1 100644 --- a/InvenTree/part/serializers.py +++ b/InvenTree/part/serializers.py @@ -119,6 +119,7 @@ class BomItemSerializer(InvenTreeModelSerializer): def setup_eager_loading(queryset): queryset = queryset.prefetch_related('sub_part') queryset = queryset.prefetch_related('sub_part__category') + queryset = queryset.prefetch_related('sub_part__locations') return queryset class Meta: diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index 3c08ff8822..2659d3702d 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -282,6 +282,9 @@ class StockList(generics.ListCreateAPIView): if supplier_id: stock_list = stock_list.filter(supplier_part__supplier=supplier_id) + # Pre-fetch related objects for better response time + stock_list = self.get_serializer_class().setup_eager_loading(stock_list) + return stock_list serializer_class = StockItemSerializer diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index a0461ff409..46a1c44845 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -60,6 +60,15 @@ class StockItemSerializer(serializers.ModelSerializer): location = LocationBriefSerializer(many=False, read_only=True) status_text = serializers.CharField(source='get_status_display', read_only=True) + @staticmethod + def setup_eager_loading(queryset): + queryset = queryset.prefetch_related('part') + queryset = queryset.prefetch_related('part__locations') + queryset = queryset.prefetch_related('part__category') + queryset = queryset.prefetch_related('location') + + return queryset + class Meta: model = StockItem fields = [