Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add prefetching and improve readability
Marcin Gębala19efd7806198
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 700d3d3a73..cfd10c9cfb 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -246,9 +246,9 @@ class ProductCreate(ModelMutation):if instance.pk else cleaned_input.get('product_type'))if attributes and product_type:+ qs = product_type.product_attributes.prefetch_related('values')try:- attributes = attributes_to_hstore(- attributes, product_type.product_attributes.all())+ attributes = attributes_to_hstore(attributes, qs)except ValueError as e:cls.add_error(errors, 'attributes', str(e))else:@@ -340,8 +340,8 @@ class ProductVariantCreate(ModelMutation):if attributes and product_type:try:- attributes = attributes_to_hstore(- attributes, product_type.variant_attributes.all())+ qs = product_type.variant_attributes.prefetch_related('values')+ attributes = attributes_to_hstore(attributes, qs)except ValueError as e:cls.add_error(errors, 'attributes', str(e))else:diff --git a/saleor/graphql/product/utils.py b/saleor/graphql/product/utils.pyindex 1b735c1767..6434486239 100644--- a/saleor/graphql/product/utils.py+++ b/saleor/graphql/product/utils.py@@ -12,7 +12,7 @@ def attributes_to_hstore(attribute_value_input, attributes_queryset):a dict of IDs. This function transforms the list of `AttributeValueInput`objects to this format."""- attributes_map = dict(attributes_queryset.values_list('slug', 'id'))+ attributes_map = {attr.slug: attr.id for attr in attributes_queryset}attributes_hstore = {}values_map = dict(@@ -36,9 +36,8 @@ def attributes_to_hstore(attribute_value_input, attributes_queryset):# `value_id` was not found; create a new AttributeChoiceValue# instance from the provided `value`.attr_instance = ProductAttribute.objects.get(slug=attr_slug)- obj = AttributeChoiceValue.objects.get_or_create(- attribute_id=attr_instance.pk, name=value,- slug=slugify(value))[0]+ obj = attr_instance.values.get_or_create(+ name=value, slug=slugify(value))[0]value_id = obj.pkattributes_hstore[str(attribute_id)] = str(value_id)