Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Reflect attribute sorting in 1.0 applications
NyanKiyoshi87e49a812a91
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex ac294e7872..98f3c7a88a 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -113,8 +113,12 @@ class ProductTypeForm(forms.ModelForm):variant_attrs_qs = product_attrs_qs = Attribute.objects.all()if self.instance.pk:- product_attrs_initial = self.instance.product_attributes.all()- variant_attrs_initial = self.instance.variant_attributes.all()+ product_attrs_initial = (+ self.instance.product_attributes.all().product_attributes_sorted()+ )+ variant_attrs_initial = (+ self.instance.variant_attributes.all().variant_attributes_sorted()+ )else:product_attrs_initial = []variant_attrs_initial = []@@ -275,7 +279,7 @@ class ProductForm(forms.ModelForm, AttributesMixin):)self.available_attributes = product_type.product_attributes.prefetch_related("values"- ).all()+ ).product_attributes_sorted()self.prepare_fields_for_attributes()self.fields["collections"].initial = Collection.objects.filter(products__name=self.instance@@ -371,8 +375,10 @@ class ProductVariantForm(forms.ModelForm, AttributesMixin):self.fields["price_override"].widget.attrs["placeholder"] = self.instance.product.price.amount- qs = self.instance.product.product_type.variant_attributes.all()- self.available_attributes = qs.prefetch_related("values")+ qs = self.instance.product.product_type.variant_attributes+ self.available_attributes = qs.prefetch_related(+ "values"+ ).variant_attributes_sorted()self.prepare_fields_for_attributes()if include_taxes_in_prices():diff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex b5f06d09ee..2dbce15d79 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -152,7 +152,10 @@ def product_create(request, type_pk):@staff_member_required@permission_required("product.manage_products")def product_edit(request, pk):- product = get_object_or_404(Product.objects.prefetch_related("variants"), pk=pk)+ product = get_object_or_404(+ Product.objects.prefetch_related("variants", "product_type__attributeproduct"),+ pk=pk,+ )form = forms.ProductForm(request.POST or None, instance=product)edit_variant = not product.product_type.has_variantsif edit_variant:diff --git a/saleor/product/utils/attributes.py b/saleor/product/utils/attributes.pyindex eca04b5284..85af2c18b0 100644--- a/saleor/product/utils/attributes.py+++ b/saleor/product/utils/attributes.py@@ -9,7 +9,8 @@ def get_product_attributes_data(product):"""attributes = product.product_type.product_attributes.exclude(visible_in_storefront=False- )+ ).product_attributes_sorted()+attributes_map = {attribute.pk: attribute.translated for attribute in attributes}values_map = get_attributes_display_map(product, attributes)product_attributes = {}diff --git a/saleor/product/utils/variants_picker.py b/saleor/product/utils/variants_picker.pyindex 48414dcf61..b1fc2d0c5a 100644--- a/saleor/product/utils/variants_picker.py+++ b/saleor/product/utils/variants_picker.py@@ -44,7 +44,9 @@ def get_variant_picker_data(variants = product.variants.all()data = {"variantAttributes": [], "variants": []}- variant_attributes = product.product_type.variant_attributes.all()+ variant_attributes = (+ product.product_type.variant_attributes.all().variant_attributes_sorted()+ )# Collect only available variantsfilter_available_variants = defaultdict(list)diff --git a/templates/dashboard/product/product_type/list.html b/templates/dashboard/product/product_type/list.htmlindex 5c6d42216d..1001fe2726 100644--- a/templates/dashboard/product/product_type/list.html+++ b/templates/dashboard/product/product_type/list.html@@ -60,14 +60,14 @@</td><td>{% if product_attributes %}- {{ product_attributes|join:", " }}+ {{ product_attributes.product_attributes_sorted|join:", " }}{% else %}-{% endif %}</td><td>{% if variant_attributes %}- {{ variant_attributes|join:", " }}+ {{ variant_attributes.variant_attributes_sorted|join:", " }}{% else %}-{% endif %}diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.pyindex a1a04e4fa0..cbccde6837 100644--- a/tests/api/test_attributes.py+++ b/tests/api/test_attributes.py@@ -1508,7 +1508,7 @@ def test_sort_attributes_within_product_type(m2m_attributes = getattr(product_type, relation_field)m2m_attributes.set(attributes)- sort_method = getattr(m2m_attributes, f"{relation_field}_sorted_for_dashboard")+ sort_method = getattr(m2m_attributes, f"{relation_field}_sorted")attributes = list(sort_method())assert len(attributes) == 3