Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Set initials for multiple ajax select2 field
Anna Siwik4e8e8f75f4a8
diff --git a/saleor/core/forms.py b/saleor/core/forms.pyindex 93166fb33d..de4330c3d5 100644--- a/saleor/core/forms.py+++ b/saleor/core/forms.py@@ -1,3 +1,5 @@+import json+from django import forms@@ -7,7 +9,7 @@ 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, *args, **kwargs):self.queryset = kwargs.pop('queryset')super(AjaxSelect2ChoiceField, self).__init__(*args, **kwargs)self.widget.attrs['class'] = 'enable-ajax-select2'@@ -37,7 +39,15 @@ class AjaxSelect2ChoiceField(forms.ChoiceField):class AjaxSelect2MultipleChoiceField(AjaxSelect2ChoiceField):""" Enables multiple select for AjaxSelect2ChoiceField. """- def __init__(self, fetch_data_url='', initial=None, *args, **kwargs):+ def __init__(self, fetch_data_url='', initial=[], *args, **kwargs):super(AjaxSelect2MultipleChoiceField, self).__init__(fetch_data_url, initial, *args, **kwargs)self.widget.attrs['multiple'] = True+ if initial:+ self.set_initial(initial)++ def set_initial(self, objects):+ """ Sets initially selected objects on field's widget. """+ self.widget.attrs['data-initial'] = json.dumps([+ {'id': obj.pk, 'text': str(obj)} for obj in objects+ ])diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.pyindex 5e90b6f033..b5e39ee749 100644--- a/saleor/dashboard/discount/forms.py+++ b/saleor/dashboard/discount/forms.py@@ -24,6 +24,10 @@ class SaleForm(forms.ModelForm):model = Saleexclude = []+ def __init__(self, *args, **kwargs):+ super(SaleForm, self).__init__(*args, **kwargs)+ self.fields['products'].set_initial(self.instance.products.all())+def clean(self):cleaned_data = super(SaleForm, self).clean()discount_type = cleaned_data['type']diff --git a/saleor/static/dashboard/js/components/utils.js b/saleor/static/dashboard/js/components/utils.jsindex 2c4661728e..65cd9bf430 100644--- a/saleor/static/dashboard/js/components/utils.js+++ b/saleor/static/dashboard/js/components/utils.js@@ -22,6 +22,7 @@ export function initSelects() {let $ajaxSelect2Elements = $('select.enable-ajax-select2');$ajaxSelect2Elements.each(function() {let $select = $(this);+if ($select.attr('data-initial-display') &&$select.attr('data-initial-value')) {let display = $select.data('initial-display');@@ -31,6 +32,17 @@ export function initSelects() {$select.val(value);};+ if ($select.attr('data-initial')) {+ let initial = $select.data('initial');+ let selected = []+ initial.forEach(function(item) {+ $select.append($('<option></option>').attr(+ 'value', item.id).text(item.text));+ selected.push(item.id);+ });+ $select.val(selected);+ };+let url = $select.data('url');$select.select2({ajax: {