diff --git a/backend/api/orders_serializers.py b/backend/api/orders_serializers.py index a598e75..8ca02e7 100644 --- a/backend/api/orders_serializers.py +++ b/backend/api/orders_serializers.py @@ -1,6 +1,7 @@ import json import re +from django.db.models import Prefetch from rest_framework import serializers from .products_serializers import ProductPresentSerializer @@ -92,24 +93,17 @@ class Meta: ) model = Order - # TODO: commented code doesn't reduce DB hits @classmethod def setup_eager_loading(cls, queryset): """Perform necessary eager loading of orders data.""" - return queryset.select_related("user") - # ).prefetch_related( - # Prefetch( - # "products", - # queryset=Product.objects.select_related( - # "category", "subcategory", "producer" - # ).prefetch_related( - # "components", - # "tags", - # "promotions", - # "reviews", - # ), - # ) - # ) + return queryset.select_related("user").prefetch_related( + Prefetch( + "orders", + queryset=OrderProduct.objects.select_related( + "product__category" + ).prefetch_related("product__promotions"), + ) + ) class OrderGetAnonSerializer(serializers.ModelSerializer): diff --git a/backend/api/orders_views.py b/backend/api/orders_views.py index fd50759..4d1f73a 100644 --- a/backend/api/orders_views.py +++ b/backend/api/orders_views.py @@ -273,7 +273,6 @@ def retrieve(self, request, **kwargs): def list(self, request, **kwargs): if self.request.user.is_authenticated: queryset = self.get_queryset().filter(user=self.request.user) - # TODO: need to solve n+1 problem serializer = self.get_serializer(queryset, many=True) logger.info("The user's order list was successfully received.") return Response(serializer.data, status=status.HTTP_200_OK)