Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
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.pyindex 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.pyindex 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, fieldimport graphenefrom django.core.exceptions import ValidationErrorfrom django.db import transaction-from django.db.models import Q+from django.db.models import Q, prefetch_related_objectsfrom django.utils.text import slugifyfrom graphene.utils.str_converters import to_camel_casefrom graphql.error import GraphQLErrorfrom text_unidecode import unidecode-from ....attribute import AttributeEntityType, models+from ....attribute import AttributeEntityType, AttributeInputType, modelsfrom ....attribute.error_codes import AttributeBulkUpdateErrorCodefrom ....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)@classmethoddiff --git a/saleor/graphql/page/mutations/page_create.py b/saleor/graphql/page/mutations/page_create.pyindex 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 castimport graphenefrom django.core.exceptions import ValidationError+from django.db.models import QuerySetfrom ....core.tracing import traced_atomic_transactionfrom ....page import models@@ -68,7 +70,8 @@ class PageCreate(DeprecatedModelMutation):@classmethoddef 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.pyindex 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, QuerySetfrom ....attribute import models as attribute_modelsfrom ....core.utils.update_mutation_manager import InstanceTracker@@ -36,7 +38,8 @@ class PageUpdate(PageCreate):@classmethoddef 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)