Skip to content

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

Fix attribute over-fetching for pages (#18428) (#18494)

Iga Karbowiak6e436dcd64e7
diff --git a/saleor/graphql/attribute/mutations/attribute_bulk_create.py b/saleor/graphql/attribute/mutations/attribute_bulk_create.py
index 41ab8e6ea9..099a50f879 100644
--- a/saleor/graphql/attribute/mutations/attribute_bulk_create.py
+++ b/saleor/graphql/attribute/mutations/attribute_bulk_create.py
@@ -98,9 +98,6 @@ def clean_values(
cleand_values: list = []
slugs_list: list = []
- if attribute:
- slugs_list = list(attribute.values.values_list("slug", flat=True))
-
duplicated_names = get_duplicated_values(
[
unidecode(value_data.name.lower().strip())
@@ -175,6 +172,9 @@ def clean_values(
)
continue
+ if attribute:
+ slugs_list = [value.slug for value in attribute.values.all()]
+
validate_value(
value_data,
input_type,
diff --git a/saleor/graphql/attribute/mutations/attribute_bulk_update.py b/saleor/graphql/attribute/mutations/attribute_bulk_update.py
index 64f79a3859..f2b4bc9cbd 100644
--- a/saleor/graphql/attribute/mutations/attribute_bulk_update.py
+++ b/saleor/graphql/attribute/mutations/attribute_bulk_update.py
@@ -4,13 +4,13 @@ from dataclasses import dataclass, field
import graphene
from django.core.exceptions import ValidationError
from django.db import transaction
-from django.db.models import Q
+from django.db.models import Q, prefetch_related_objects
from django.utils.text import slugify
from graphene.utils.str_converters import to_camel_case
from graphql.error import GraphQLError
from text_unidecode import unidecode
-from ....attribute import AttributeEntityType, models
+from ....attribute import AttributeEntityType, AttributeInputType, models
from ....attribute.error_codes import AttributeBulkUpdateErrorCode
from ....attribute.lock_objects import (
attribute_reference_page_types_qs_select_for_update,
@@ -632,7 +632,13 @@ class AttributeBulkUpdate(BaseMutation):
else:
return []
- attributes = models.Attribute.objects.filter(lookup).prefetch_related("values")
+ attributes = list(models.Attribute.objects.filter(lookup))
+ attributes_with_choices = [
+ attr
+ for attr in attributes
+ if attr.input_type in AttributeInputType.TYPES_WITH_CHOICES
+ ]
+ prefetch_related_objects(attributes_with_choices, "values")
return list(attributes)
@classmethod
diff --git a/saleor/graphql/page/mutations/page_create.py b/saleor/graphql/page/mutations/page_create.py
index e2adbd3a76..78033b630d 100644
--- a/saleor/graphql/page/mutations/page_create.py
+++ b/saleor/graphql/page/mutations/page_create.py
@@ -1,7 +1,9 @@
import datetime
+from typing import cast
import graphene
from django.core.exceptions import ValidationError
+from django.db.models import QuerySet
from ....core.tracing import traced_atomic_transaction
from ....page import models
@@ -68,7 +70,8 @@ class PageCreate(DeprecatedModelMutation):
@classmethod
def clean_attributes(cls, attributes: list[dict], page_type: models.PageType):
- attributes_qs = page_type.page_attributes.prefetch_related("values")
+ attributes_qs = page_type.page_attributes
+ attributes_qs = cast(QuerySet, attributes_qs)
cleaned_attributes = AttributeAssignmentMixin.clean_input(
attributes, attributes_qs, is_page_attributes=True
)
diff --git a/saleor/graphql/page/mutations/page_update.py b/saleor/graphql/page/mutations/page_update.py
index 2bff99aea4..a3120d9807 100644
--- a/saleor/graphql/page/mutations/page_update.py
+++ b/saleor/graphql/page/mutations/page_update.py
@@ -1,5 +1,7 @@
+from typing import cast
+
import graphene
-from django.db.models import Exists, OuterRef
+from django.db.models import Exists, OuterRef, QuerySet
from ....attribute import models as attribute_models
from ....core.utils.update_mutation_manager import InstanceTracker
@@ -36,7 +38,8 @@ class PageUpdate(PageCreate):
@classmethod
def clean_attributes(cls, attributes: list[dict], page_type: models.PageType):
- attributes_qs = page_type.page_attributes.prefetch_related("values")
+ attributes_qs = page_type.page_attributes
+ attributes_qs = cast(QuerySet, attributes_qs)
cleaned_attributes = AttributeAssignmentMixin.clean_input(
attributes, attributes_qs, creation=False, is_page_attributes=True
)