2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Cleanup BomItemSerializer (#9017)

- Consolidate functions
- Code cleanup
This commit is contained in:
Oliver 2025-02-03 13:50:17 +11:00 committed by GitHub
parent b2484c54a4
commit a760d00c96
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 30 deletions

View File

@ -1863,7 +1863,6 @@ class BomMixin:
"""Return the queryset object for this endpoint.""" """Return the queryset object for this endpoint."""
queryset = super().get_queryset(*args, **kwargs) queryset = super().get_queryset(*args, **kwargs)
queryset = self.get_serializer_class().setup_eager_loading(queryset)
queryset = self.get_serializer_class().annotate_queryset(queryset) queryset = self.get_serializer_class().annotate_queryset(queryset)
return queryset return queryset

View File

@ -1696,35 +1696,6 @@ class BomItemSerializer(
external_stock = serializers.FloatField(read_only=True) external_stock = serializers.FloatField(read_only=True)
@staticmethod
def setup_eager_loading(queryset):
"""Prefetch against the provided queryset to speed up database access."""
queryset = queryset.prefetch_related('part')
queryset = queryset.prefetch_related('part__category')
queryset = queryset.prefetch_related('part__stock_items')
queryset = queryset.prefetch_related('sub_part')
queryset = queryset.prefetch_related('sub_part__category')
queryset = queryset.prefetch_related('sub_part__pricing_data')
queryset = queryset.prefetch_related(
'sub_part__stock_items',
'sub_part__stock_items__allocations',
'sub_part__stock_items__sales_order_allocations',
)
queryset = queryset.select_related(
'part__pricing_data', 'sub_part__pricing_data'
)
queryset = queryset.prefetch_related(
'substitutes', 'substitutes__part__stock_items'
)
queryset = queryset.prefetch_related('sub_part__builds')
return queryset
@staticmethod @staticmethod
def annotate_queryset(queryset): def annotate_queryset(queryset):
"""Annotate the BomItem queryset with extra information. """Annotate the BomItem queryset with extra information.
@ -1737,6 +1708,22 @@ class BomItemSerializer(
available_stock = total_stock - build_order_allocations - sales_order_allocations available_stock = total_stock - build_order_allocations - sales_order_allocations
""" """
# Prefetch related fields
queryset = queryset.prefetch_related(
'part',
'part__category',
'part__stock_items',
'sub_part',
'sub_part__builds',
'sub_part__category',
'sub_part__pricing_data',
'sub_part__stock_items',
'sub_part__stock_items__allocations',
'sub_part__stock_items__sales_order_allocations',
'substitutes',
'substitutes__part__stock_items',
).select_related('part__pricing_data', 'sub_part__pricing_data')
# Annotate with the 'total pricing' information based on unit pricing and quantity # Annotate with the 'total pricing' information based on unit pricing and quantity
queryset = queryset.annotate( queryset = queryset.annotate(
pricing_min_total=ExpressionWrapper( pricing_min_total=ExpressionWrapper(