Skip to content

Commit

Permalink
Support data_view and data_url class attributes on HeavySelect (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
CleitonDeLima authored Feb 8, 2023
1 parent 38ce1d6 commit 0681174
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 4 additions & 2 deletions django_select2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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:
Expand Down
14 changes: 10 additions & 4 deletions tests/testapp/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit 0681174

Please sign in to comment.