Optimized catalog view: filter only active products/kits in prefetch
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
"""
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.views.generic import TemplateView
|
||||
from django.db.models import Prefetch
|
||||
|
||||
from ..models import Product, ProductKit, ProductCategory
|
||||
|
||||
@@ -26,10 +27,20 @@ class CatalogView(LoginRequiredMixin, TemplateView):
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
# Все активные категории
|
||||
# Оптимизированный prefetch только для активных товаров и комплектов
|
||||
active_products_prefetch = Prefetch(
|
||||
'products',
|
||||
queryset=Product.objects.filter(status='active').order_by('name')
|
||||
)
|
||||
active_kits_prefetch = Prefetch(
|
||||
'kits',
|
||||
queryset=ProductKit.objects.filter(status='active', is_temporary=False).order_by('name')
|
||||
)
|
||||
|
||||
# Все активные категории с prefetch только активных товаров
|
||||
categories = list(ProductCategory.objects.filter(
|
||||
is_active=True, is_deleted=False
|
||||
).prefetch_related('products', 'kits').order_by('name'))
|
||||
).prefetch_related(active_products_prefetch, active_kits_prefetch).order_by('name'))
|
||||
|
||||
# Строим дерево
|
||||
category_tree = self.build_category_tree(categories, parent=None)
|
||||
|
||||
Reference in New Issue
Block a user