From 0681174acabfd30e1baf2cc0d99d9f0a116e2a5a Mon Sep 17 00:00:00 2001 From: Cleiton de Lima Date: Wed, 8 Feb 2023 10:45:22 -0300 Subject: [PATCH] Support data_view and data_url class attributes on HeavySelect (#182) --- django_select2/forms.py | 6 ++++-- tests/testapp/forms.py | 14 ++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/django_select2/forms.py b/django_select2/forms.py index eaea7743..045693e1 100644 --- a/django_select2/forms.py +++ b/django_select2/forms.py @@ -229,6 +229,8 @@ class HeavySelect2Mixin: """Mixin that adds select2's AJAX options and registers itself on Django's cache.""" dependent_fields = {} + data_view = None + data_url = None def __init__(self, attrs=None, choices=(), **kwargs): """ @@ -249,8 +251,8 @@ def __init__(self, attrs=None, choices=(), **kwargs): self.uuid = str(uuid.uuid4()) self.field_id = signing.dumps(self.uuid) - self.data_view = kwargs.pop("data_view", None) - self.data_url = kwargs.pop("data_url", None) + self.data_view = kwargs.pop("data_view", self.data_view) + self.data_url = kwargs.pop("data_url", self.data_url) dependent_fields = kwargs.pop("dependent_fields", None) if dependent_fields is not None: diff --git a/tests/testapp/forms.py b/tests/testapp/forms.py index da9487c5..ed90f222 100644 --- a/tests/testapp/forms.py +++ b/tests/testapp/forms.py @@ -51,6 +51,14 @@ def label_from_instance(self, obj): return force_str(obj.title).upper() +class ArtistDataViewWidget(HeavySelect2Widget): + data_view = "heavy_data_1" + + +class PrimaryGenreDataUrlWidget(HeavySelect2Widget): + data_url = "/heavy_data_2/" + + class AlbumSelect2WidgetForm(forms.ModelForm): class Meta: model = models.Album @@ -143,11 +151,9 @@ class Select2WidgetForm(forms.Form): class HeavySelect2WidgetForm(forms.Form): - artist = forms.ChoiceField( - widget=HeavySelect2Widget(data_view="heavy_data_1"), choices=NUMBER_CHOICES - ) + artist = forms.ChoiceField(widget=ArtistDataViewWidget(), choices=NUMBER_CHOICES) primary_genre = forms.ChoiceField( - widget=HeavySelect2Widget(data_view="heavy_data_2"), + widget=PrimaryGenreDataUrlWidget(), required=False, choices=NUMBER_CHOICES, )