Skip to content

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

Applied changes from reviews

NyanKiyoshia99255c4f05f
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index f5cd4beaff..3826fa3057 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -201,13 +201,6 @@ class AttributesMixin:
available_attributes = Attribute.objects.none()
- def __init__(self, *args, **kwargs):
- if not self.instance_attributes_rel:
- raise Exception(
- "instance_attributes_rel must be set in subclasses of "
- "AttributesMixin."
- )
-
def prepare_fields_for_attributes(self):
initial_attrs = self.instance.attributes
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index 4238eb543a..1139fbf6f4 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -8,7 +8,6 @@ from django.core.exceptions import (
ImproperlyConfigured,
ValidationError,
)
-from django.db.models import QuerySet
from django.db.models.fields.files import FileField
from graphene import ObjectType
from graphene.types.mutation import MutationOptions
@@ -356,17 +355,15 @@ class ModelMutation(BaseMutation):
instance.save()
@classmethod
- def get_instance(cls, info, __qs: QuerySet = None, **data):
+ def get_instance(cls, info, **data):
"""Retrieve an instance from the supplied global id.
- Optionally take a __qs parameter to resolve the node using a custom query set.
+ Optionally take a qs parameter to resolve the node using a custom query set.
"""
object_id = data.get("id")
if object_id:
model_type = registry.get_type_for_model(cls._meta.model)
- instance = cls.get_node_or_error(
- info, object_id, only_type=model_type, qs=__qs
- )
+ instance = cls.get_node_or_error(info, object_id, only_type=model_type)
else:
instance = cls._meta.model()
return instance
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index f3e58dfb5e..36b7cf824d 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -631,6 +631,8 @@ class AttributeAssignmentMixin:
for attribute in attributes:
key = pks.get(attribute.pk, None)
+ # Retrieve the primary key by slug if it
+ # was not resolved through a global ID but a slug
if key is None:
key = slugs[attribute.slug]
@@ -733,16 +735,21 @@ class ProductCreate(ModelMutation):
raise ValidationError({"sku": "Product with this SKU already exists."})
@classmethod
- def get_instance(cls, info, __qs: QuerySet = None, **data):
+ def get_instance(cls, info, **data):
"""Prefetch related fields that are needed to process the mutation."""
# If we are updating an instance and want to update its attributes,
# prefetch them.
- if data.get("id") and data.get("attributes"):
- __qs = cls.Meta.model.objects.prefetch_related(
- "product_type__product_attributes",
- "attributes__assignment__attribute__values",
+
+ object_id = data.get("id")
+ if object_id and data.get("attributes"):
+ # Prefetches needed by AttributeAssignmentMixin and
+ # associate_attribute_values_to_instance
+ qs = cls.Meta.model.objects.prefetch_related(
+ "product_type__product_attributes", "product_type__attributeproduct"
)
- return super().get_instance(info, __qs, **data)
+ return cls.get_node_or_error(info, object_id, only_type="Product", qs=qs)
+
+ return super().get_instance(info, **data)
@classmethod
@transaction.atomic
@@ -952,6 +959,28 @@ class ProductVariantCreate(ModelMutation):
raise ValidationError({"attributes": str(exc.args[0])})
return cleaned_input
+ @classmethod
+ def get_instance(cls, info, **data):
+ """Prefetch related fields that are needed to process the mutation.
+
+ If we are updating an instance and want to update its attributes,
+ # prefetch them.
+ """
+
+ object_id = data.get("id")
+ if object_id and data.get("attributes"):
+ # Prefetches needed by AttributeAssignmentMixin and
+ # associate_attribute_values_to_instance
+ qs = cls.Meta.model.objects.prefetch_related(
+ "product__product_type__variant_attributes",
+ "product__product_type__attributevariant",
+ )
+ return cls.get_node_or_error(
+ info, object_id, only_type="ProductVariant", qs=qs
+ )
+
+ return super().get_instance(info, **data)
+
@classmethod
@transaction.atomic()
def save(cls, info, instance, cleaned_input):