2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 20:16:44 +00:00

Significant query speed improvements to stock list API

- Thanks, django-debug-toolbar!
- Gah, django DRF is the worst. Enforcing a PrimaryKeyRelatedSerializer seems to really improve speed
This commit is contained in:
Oliver Walters 2020-08-15 21:15:11 +10:00
parent 411e7507a3
commit 2f77007dbe
2 changed files with 26 additions and 10 deletions

View File

@ -338,11 +338,6 @@ class StockList(generics.ListCreateAPIView):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
data = serializer.data
@ -363,6 +358,7 @@ class StockList(generics.ListCreateAPIView):
part_ids.add(part)
sp = item['supplier_part']
if sp:
supplier_part_ids.add(sp)
@ -434,6 +430,7 @@ class StockList(generics.ListCreateAPIView):
def get_queryset(self, *args, **kwargs):
queryset = super().get_queryset(*args, **kwargs)
queryset = StockItemSerializer.prefetch_queryset(queryset)
queryset = StockItemSerializer.annotate_queryset(queryset)

View File

@ -99,15 +99,34 @@ class StockItemSerializer(InvenTreeModelSerializer):
return queryset
belongs_to = serializers.PrimaryKeyRelatedField(read_only=True)
build_order = serializers.PrimaryKeyRelatedField(read_only=True)
customer = serializers.PrimaryKeyRelatedField(read_only=True)
location = serializers.PrimaryKeyRelatedField(read_only=True)
in_stock = serializers.BooleanField(read_only=True)
sales_order = serializers.PrimaryKeyRelatedField(read_only=True)
status_text = serializers.CharField(source='get_status_display', read_only=True)
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
location_detail = LocationBriefSerializer(source='location', many=False, read_only=True)
supplier_part = serializers.PrimaryKeyRelatedField(read_only=True)
supplier_part_detail = SupplierPartSerializer(source='supplier_part', many=False, read_only=True)
part = serializers.PrimaryKeyRelatedField(read_only=True)
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
location_detail = LocationBriefSerializer(source='location', many=False, read_only=True)
tracking_items = serializers.IntegerField(source='tracking_info_count', read_only=True, required=False)
quantity = serializers.FloatField()
allocated = serializers.FloatField(source='allocation_count', required=False)
serial = serializers.IntegerField(required=False)
@ -140,9 +159,9 @@ class StockItemSerializer(InvenTreeModelSerializer):
fields = [
'allocated',
'batch',
'build_order',
'belongs_to',
'customer',
'build_order',
'in_stock',
'link',
'location',
@ -155,10 +174,10 @@ class StockItemSerializer(InvenTreeModelSerializer):
'required_tests',
'sales_order',
'serial',
'supplier_part',
'supplier_part_detail',
'status',
'status_text',
'supplier_part',
'supplier_part_detail',
'tracking_items',
'uid',
]