Skip to content

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

Describe AjaxSelect2ChoiceField in docstring

Anna Siwik5fd574b979fe
diff --git a/saleor/core/forms.py b/saleor/core/forms.py
index 95206486c5..725fa012b1 100644
--- a/saleor/core/forms.py
+++ b/saleor/core/forms.py
@@ -2,6 +2,10 @@ from django import forms
class AjaxSelect2ChoiceField(forms.ChoiceField):
+ """ This field enables usage of select2 with ajax fetched data.
+
+ fetch_data_url - specifies url, from which select2 will fetch data
+ """
def __init__(self, fetch_data_url='', initial=None, *args, **kwargs):
self.queryset = kwargs.pop('queryset')
@@ -25,9 +29,7 @@ class AjaxSelect2ChoiceField(forms.ChoiceField):
forms.Field.validate(self, value)
return True
- def set_fetch_data_url(self, url):
- self.widget.attrs['data-url'] = url
-
def set_initial(self, obj):
+ """ Sets initially selected object on field's widget. """
self.widget.attrs['data-initial-display'] = str(obj)
self.widget.attrs['data-initial-value'] = obj.pk
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index f51821a719..b9f380f718 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -3,7 +3,7 @@ from __future__ import unicode_literals
from django import forms
from django.conf import settings
from django.core.validators import MinValueValidator, MaxValueValidator
-from django.shortcuts import reverse
+from django.urls import reverse_lazy
from django.utils.translation import npgettext_lazy, pgettext_lazy
from django_prices.forms import PriceField
from payments import PaymentError, PaymentStatus
@@ -355,7 +355,9 @@ class ChangeStockForm(forms.ModelForm):
class AddDeliveryGroupItemForm(forms.Form):
- variant = AjaxSelect2ChoiceField(queryset=ProductVariant.objects.all())
+ variant = AjaxSelect2ChoiceField(
+ queryset=ProductVariant.objects.all(),
+ fetch_data_url=reverse_lazy('dashboard:ajax-variants'))
quantity = QuantityField(
label=pgettext_lazy('Variant quantity form label', 'Quantity'),
validators=[MinValueValidator(1)])
@@ -363,8 +365,6 @@ class AddDeliveryGroupItemForm(forms.Form):
def __init__(self, *args, **kwargs):
self.group = kwargs.pop('group')
super(AddDeliveryGroupItemForm, self).__init__(*args, **kwargs)
- self.fields['variant'].set_fetch_data_url(
- reverse('dashboard:ajax-variants'))
def clean(self):
cleaned_data = super(AddDeliveryGroupItemForm, self).clean()