Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Refactor filters
michal7a8cb009d44c
diff --git a/saleor/product/filters.py b/saleor/product/filters.pyindex 68f785e193..170c6a1b6a 100644--- a/saleor/product/filters.py+++ b/saleor/product/filters.py@@ -9,11 +9,9 @@ from .models import Product, AttributeChoiceValueclass ProductFilter(FilterSet):def __init__(self, *args, **kwargs):super(ProductFilter, self).__init__(*args, **kwargs)- self.products = self.queryset-product_attributes = set()variant_attributes = set()- for product in self.products:+ for product in self.queryset:for attribute in product.product_class.variant_attributes.all():product_attributes.add(attribute)for attribute in product.product_class.product_attributes.all():@@ -25,8 +23,7 @@ class ProductFilter(FilterSet):name='product_class__product_attributes__values__slug',label=attribute.name,widget=CheckboxSelectMultiple,- choices=self.get_attribute_choices(attribute)- )+ choices=get_attribute_choices(attribute))for attribute in variant_attributes:self.filters[attribute.slug] = \@@ -34,14 +31,7 @@ class ProductFilter(FilterSet):name='product_class__variant_attributes__values__slug',label=attribute.name,widget=CheckboxSelectMultiple,- choices=self.get_attribute_choices(attribute)- )-- def get_attribute_choices(self, attribute):- result = [(choice.slug, choice.name)- for choice in AttributeChoiceValue.objects- .filter(attribute__name=attribute.name)]- return result+ choices=get_attribute_choices(attribute))class Meta:model = Product@@ -52,3 +42,10 @@ class ProductFilter(FilterSet):'filter_class': RangeFilter}}+++def get_attribute_choices(self, attribute):+ result = [(choice.slug, choice.name)+ for choice in AttributeChoiceValue.objects+ .filter(attribute__name=attribute.name)]+ return resultdiff --git a/saleor/product/views.py b/saleor/product/views.pyindex f820908d51..a5a8c22217 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -114,10 +114,10 @@ def category_index(request, path, category_id):category = get_object_or_404(Category, id=category_id)actual_path = category.get_full_path()- products = (Product.objects.all()- .filter(categories__name=category)+ products = (Product.objects.filter(categories__name=category).prefetch_related(- 'images', 'product_class__variant_attributes__values',+ 'images',+ 'product_class__variant_attributes__values','product_class__product_attributes__values','variants__stock').all())