🐛 Defining 'attrs' and 'data_view' as classes var on a custom widget doesn't doesn't work #338
Replies: 3 comments
-
Adding this as the first line in
And changing line 395 in forms.py to this should fix the
|
Beta Was this translation helpful? Give feedback.
-
Hi @coredumperror 👋, Thanks for reaching out! You are right; currently we don't support adding attributes to class. This could be added. However, you might want to consider mutability. A class holding a mutable dictionary carries some risks. I am a bit hesitant to add new features to avoid feature bloat. That being said, I am a big fan of inheritance and people being able to quickly add the features they need. Your addition falls into this category. However, it might be something that should be added to Django, not this package. If you propose this change on the Django form or as a ticket, I will make sure to boost it and help you with the Django integration. Best |
Beta Was this translation helpful? Give feedback.
-
OK, I did as you suggested and created a Django feature request for the Widget class: django/new-features#72. And my suggested implementation should deal with the problem of a mutable class var, too. However, there is still the issue of the
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Bug Description
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 formodel
andqueryset
, I assumed the same could be done forattrs
andcata_view
.But it can't. Any
attrs
class var gets overwritten by{}
, due to what I believe qualifies as a bug inHeavySelect2Mixin
, anddata_view
gets overwritten by a bug inModelSelect2Mixin
.Steps to Reproduce
attrs
anddata_view
class variable, e.g.:attrs
are not applied in the HTML, and thedata_view
is not used to build the select list (the "auto" view is used instead).Expected Behavior
I expected an
attrs
anddata_view
class vars to apply themselves in the same way that anattrs
anddata_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 sendsNone
as theattrs
kwarg tosuper().__init__()
, which eventually gets toWidget.__init__()
, which setsself.attrs
to{}
, overwriting the original class var before it can be used byWidget.get_context()
, which sends it toWidget.build_attrs()
.The
data_view
problem happens inModelSelect2Mixin.__init__()
, where it setsdefaults['data_view']
to"django_select2:auto-json"
, never even trying to read thedata_view
class var to get the value.This makes it impossible to create canned Widget classes with a custom
attrs
ordata_view
for use by multiple different forms, since the only way to specifyattrs
anddata_view
is via kwarg.Beta Was this translation helpful? Give feedback.
All reactions