Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Refactor products filtering (#7379)

Filip Owczarek7567234286ba
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index 7651b05ae7..d7d44fe232 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -22,6 +22,7 @@ from ...channel.models import Channel
from ...product.models import (
Category,
Collection,
+ CollectionProduct,
Product,
ProductChannelListing,
ProductType,
@@ -38,9 +39,10 @@ from ..core.filters import (
)
from ..core.types import ChannelFilterInputObjectType, FilterInputObjectType
from ..core.types.common import IntRangeInput, PriceRangeInput
-from ..utils import get_nodes, resolve_global_ids_to_primary_keys
+from ..utils import resolve_global_ids_to_primary_keys
from ..utils.filters import filter_fields_containing_value, filter_range_field
from ..warehouse import types as warehouse_types
+from . import types as product_types
from .enums import (
CollectionPublished,
ProductTypeConfigurable,
@@ -155,7 +157,7 @@ def filter_products_by_attributes(qs, filter_values, filter_range_values):
def filter_products_by_variant_price(qs, channel_slug, price_lte=None, price_gte=None):
- channels = Channel.objects.filter(slug=channel_slug)
+ channels = Channel.objects.filter(slug=channel_slug).values("pk")
product_variant_channel_listings = ProductVariantChannelListing.objects.filter(
Exists(channels.filter(pk=OuterRef("channel_id")))
)
@@ -167,40 +169,49 @@ def filter_products_by_variant_price(qs, channel_slug, price_lte=None, price_gte
product_variant_channel_listings = product_variant_channel_listings.filter(
Q(price_amount__gte=price_gte) | Q(price_amount__isnull=True)
)
+ product_variant_channel_listings = product_variant_channel_listings.values(
+ "variant_id"
+ )
variants = ProductVariant.objects.filter(
Exists(product_variant_channel_listings.filter(variant_id=OuterRef("pk")))
- )
+ ).values("product_id")
return qs.filter(Exists(variants.filter(product_id=OuterRef("pk"))))
def filter_products_by_minimal_price(
qs, channel_slug, minimal_price_lte=None, minimal_price_gte=None
):
+ channels = Channel.objects.filter(slug=channel_slug).values("pk")
+ product_channel_listings = ProductChannelListing.objects.filter(
+ Exists(channels.filter(pk=OuterRef("channel_id")))
+ )
if minimal_price_lte:
- qs = qs.filter(
- channel_listings__discounted_price_amount__lte=minimal_price_lte,
- channel_listings__discounted_price_amount__isnull=False,
- channel_listings__channel__slug=channel_slug,
+ product_channel_listings = product_channel_listings.filter(
+ discounted_price_amount__lte=minimal_price_lte,
+ discounted_price_amount__isnull=False,
)
if minimal_price_gte:
- qs = qs.filter(
- channel_listings__discounted_price_amount__gte=minimal_price_gte,
- channel_listings__discounted_price_amount__isnull=False,
- channel_listings__channel__slug=channel_slug,
+ product_channel_listings = product_channel_listings.filter(
+ discounted_price_amount__gte=minimal_price_gte,
+ discounted_price_amount__isnull=False,
)
- return qs
+ product_channel_listings = product_channel_listings.values("product_id")
+ return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))
-def filter_products_by_categories(qs, categories):
- categories = [
- category.get_descendants(include_self=True) for category in categories
- ]
- ids = {category.id for tree in categories for category in tree}
- return qs.filter(category__in=ids)
+def filter_products_by_categories(qs, category_ids):
+ categories = Category.objects.filter(pk__in=category_ids)
+ categories = Category.tree.get_queryset_descendants(
+ categories, include_self=True
+ ).values("pk")
+ return qs.filter(Exists(categories.filter(pk=OuterRef("category_id"))))
-def filter_products_by_collections(qs, collections):
- return qs.filter(collections__in=collections)
+def filter_products_by_collections(qs, collection_pks):
+ collection_products = CollectionProduct.objects.filter(
+ collection_id__in=collection_pks
+ ).values("product_id")
+ return qs.filter(Exists(collection_products.filter(product_id=OuterRef("pk"))))
def filter_products_by_stock_availability(qs, stock_availability, channel_slug):
@@ -241,27 +252,47 @@ def _filter_attributes(qs, _, value):
def filter_categories(qs, _, value):
if value:
- categories = get_nodes(value, "Category", Category)
- qs = filter_products_by_categories(qs, categories)
+ _, category_pks = resolve_global_ids_to_primary_keys(
+ value, product_types.Category
+ )
+ qs = filter_products_by_categories(qs, category_pks)
return qs
+def filter_product_types(qs, _, value):
+ if not value:
+ return qs
+ _, product_type_pks = resolve_global_ids_to_primary_keys(
+ value, product_types.ProductType
+ )
+ return qs.filter(product_type_id__in=product_type_pks)
+
+
+def filter_product_ids(qs, _, value):
+ if not value:
+ return qs
+ _, product_pks = resolve_global_ids_to_primary_keys(value, product_types.Product)
+ return qs.filter(id__in=product_pks)
+
+
def filter_has_category(qs, _, value):
return qs.filter(category__isnull=not value)
def filter_collections(qs, _, value):
if value:
- collections = get_nodes(value, "Collection", Collection)
- qs = filter_products_by_collections(qs, collections)
+ _, collection_pks = resolve_global_ids_to_primary_keys(
+ value, product_types.Collection
+ )
+ qs = filter_products_by_collections(qs, collection_pks)
return qs
def _filter_products_is_published(qs, _, value, channel_slug):
- channel = Channel.objects.filter(slug=channel_slug)
+ channel = Channel.objects.filter(slug=channel_slug).values("pk")
product_channel_listings = ProductChannelListing.objects.filter(
Exists(channel.filter(pk=OuterRef("channel_id"))), is_published=value
- )
+ ).values("product_id")
return qs.filter(Exists(product_channel_listings.filter(product_id=OuterRef("pk"))))
@@ -418,10 +449,10 @@ class ProductFilter(MetadataFilterBase):
stock_availability = EnumFilter(
input_class=StockAvailability, method="filter_stock_availability"
)
- product_types = GlobalIDMultipleChoiceFilter(field_name="product_type")
+ product_types = GlobalIDMultipleChoiceFilter(method=filter_product_types)
stocks = ObjectTypeFilter(input_class=ProductStockFilterInput, method=filter_stocks)
search = django_filters.CharFilter(method=filter_search)
- ids = GlobalIDMultipleChoiceFilter(field_name="id")
+ ids = GlobalIDMultipleChoiceFilter(method=filter_product_ids)
class Meta:
model = Product
diff --git a/saleor/warehouse/models.py b/saleor/warehouse/models.py
index 195d51056d..269c6856a8 100644
--- a/saleor/warehouse/models.py
+++ b/saleor/warehouse/models.py
@@ -3,10 +3,11 @@ import uuid
from typing import Set
from django.db import models
-from django.db.models import F, Sum
+from django.db.models import Exists, F, OuterRef, Sum
from django.db.models.functions import Coalesce
from ..account.models import Address
+from ..channel.models import Channel
from ..core.models import ModelWithMetadata
from ..order.models import OrderLine
from ..product.models import Product, ProductVariant
@@ -63,13 +64,23 @@ class StockQuerySet(models.QuerySet):
)
def for_channel(self, channel_slug: str):
- query_warehouse = models.Subquery(
- Warehouse.objects.filter(
- shipping_zones__channels__slug=channel_slug
- ).values("pk")
- )
- return self.select_related("product_variant", "warehouse").filter(
- warehouse__in=query_warehouse
+ ShippingZoneChannel = Channel.shipping_zones.through
+ WarehouseShippingZone = ShippingZone.warehouses.through
+ channels = Channel.objects.filter(slug=channel_slug).values("pk")
+ shipping_zone_channels = ShippingZoneChannel.objects.filter(
+ Exists(channels.filter(pk=OuterRef("channel_id")))
+ ).values("shippingzone_id")
+ warehouse_shipping_zones = WarehouseShippingZone.objects.filter(
+ Exists(
+ shipping_zone_channels.filter(
+ shippingzone_id=OuterRef("shippingzone_id")
+ )
+ )
+ ).values("warehouse_id")
+ return self.select_related("product_variant").filter(
+ Exists(
+ warehouse_shipping_zones.filter(warehouse_id=OuterRef("warehouse_id"))
+ )
)
def for_country_and_channel(self, country_code: str, channel_slug):