Reduce response time for stock list

Initial time - 6.2s
After prefetch - 4.3s
This commit is contained in:
Oliver Walters 2019-05-20 08:44:52 +10:00
parent 5055226490
commit fc75ab7420
3 changed files with 13 additions and 0 deletions

View File

@ -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:

View File

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

View File

@ -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 = [