Skip to content

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

Use ajax select2 field in add/update sale

Anna Siwika1b487e8dfbc
diff --git a/saleor/core/forms.py b/saleor/core/forms.py
index 725fa012b1..0ccf4d6f0c 100644
--- a/saleor/core/forms.py
+++ b/saleor/core/forms.py
@@ -7,11 +7,14 @@ class AjaxSelect2ChoiceField(forms.ChoiceField):
fetch_data_url - specifies url, from which select2 will fetch data
"""
- def __init__(self, fetch_data_url='', initial=None, *args, **kwargs):
+ def __init__(
+ self, fetch_data_url='', initial=None, many=False, *args, **kwargs):
self.queryset = kwargs.pop('queryset')
super(AjaxSelect2ChoiceField, self).__init__(*args, **kwargs)
self.widget.attrs['class'] = 'enable-ajax-select2'
self.widget.attrs['data-url'] = fetch_data_url
+ if many:
+ self.widget.attrs['multiple'] = True
if initial:
self.set_initial(initial)
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
index 7e326e229d..eb4520bc62 100644
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -15,6 +15,10 @@ from ...shipping.models import ShippingMethodCountry, COUNTRY_CODE_CHOICES
class SaleForm(forms.ModelForm):
+ products = AjaxSelect2ChoiceField(
+ queryset=Product.objects.get_available_products(),
+ fetch_data_url=reverse_lazy('dashboard:ajax-available-products'),
+ required=True, many=True)
class Meta:
model = Sale