Skip to content

Commit

Permalink
Merge branch 'master' into l10_crowdin
Browse files Browse the repository at this point in the history
  • Loading branch information
SchrodingersGat authored Feb 3, 2025
2 parents 3b75461 + a760d00 commit ed6515d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
1 change: 0 additions & 1 deletion src/backend/InvenTree/part/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,6 @@ def get_queryset(self, *args, **kwargs):
"""Return the queryset object for this endpoint."""
queryset = super().get_queryset(*args, **kwargs)

queryset = self.get_serializer_class().setup_eager_loading(queryset)
queryset = self.get_serializer_class().annotate_queryset(queryset)

return queryset
Expand Down
45 changes: 16 additions & 29 deletions src/backend/InvenTree/part/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1696,35 +1696,6 @@ def validate_quantity(self, quantity):

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
def annotate_queryset(queryset):
"""Annotate the BomItem queryset with extra information.
Expand All @@ -1737,6 +1708,22 @@ def annotate_queryset(queryset):
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
queryset = queryset.annotate(
pricing_min_total=ExpressionWrapper(
Expand Down

0 comments on commit ed6515d

Please sign in to comment.