You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
🐛 Defining 'attrs' and 'data_view' as classes var on a custom widget doesn't doesn't work #337
Given that the docs say you can specify search_fields via both class vars on a custom widget subclass, and as constructor kwargs to a base widget class (e.g. ModelSelect2Widget), and that looking at the code shows that you can do the same for model and queryset, I assumed the same could be done for attrs and cata_view.
But it can't. Any attrs class var gets overwritten by {}, due to what I believe qualifies as a bug in HeavySelect2Mixin, and data_view gets overwritten by a bug in ModelSelect2Mixin.
Steps to Reproduce
Define a custom Heavy widget class with an attrs and data_view class variable, e.g.:
class UsersWidget(s2forms.ModelSelect2Widget):
search_fields = [
"last_name__icontains",
"first_name__icontains",
]
attrs = {
"data-placeholder": "Search for users by name...",
"data-close-on-select": "false",
"data-width": "50%",
}
data_view = "my_data_view"
Use that Widget class in your Form.
Notice that the attrs are not applied in the HTML, and the data_view is not used to build the select list (the "auto" view is used instead).
Expected Behavior
I expected an attrs and data_view class vars to apply themselves in the same way that an attrs and data_view kwarg on the Widget's constructor would, but they instead have no effect.
This happens because if there is no attrs constructor kwarg, HeavySelect2Mixin explicitly sends None as the attrs kwarg to super().__init__(), which eventually gets to Widget.__init__(), which sets self.attrs to {}, overwriting the original class var before it can be used by Widget.get_context(), which sends it to Widget.build_attrs().
The data_view problem happens in ModelSelect2Mixin.__init__(), where it sets defaults['data_view'] to "django_select2:auto-json", never even trying to read the data_view class var to get the value.
This makes it impossible to create canned Widget classes with a custom attrs or data_view for use by multiple different forms, since the only way to specify attrs and data_view is via kwarg.
changed the title [-]🐛 Defining 'attrs' as a class var on a Heavy widget doesn't apply them to the form[/-][+]🐛 Defining 'attrs' as a class var on a custom widget doesn't apply them to the form[/+]on Aug 28, 2025
changed the title [-]🐛 Defining 'attrs' as a class var on a custom widget doesn't apply them to the form[/-][+]🐛 Defining 'attrs' and 'data_view' as classes var on a custom widget doesn't doesn't work[/+]on Aug 28, 2025
Repository owner locked and limited conversation to collaborators on Aug 31, 2025
Activity
[-]🐛 Defining 'attrs' as a class var on a Heavy widget doesn't apply them to the form[/-][+]🐛 Defining 'attrs' as a class var on a custom widget doesn't apply them to the form[/+]coredumperror commentedon Aug 28, 2025
Adding this as the first line in
HeavySelect2Mixin.__init__()should fix theattrsissue:And changing line 395 in forms.py to this should fix the
data_viewissue:[-]🐛 Defining 'attrs' as a class var on a custom widget doesn't apply them to the form[/-][+]🐛 Defining 'attrs' and 'data_view' as classes var on a custom widget doesn't doesn't work[/+]