Skip to content

Fix HeavySelect class attributes #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions django_select2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,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 @@ -247,8 +249,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