🐛 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_fieldsvia 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 formodelandqueryset, I assumed the same could be done forattrsandcata_view.But it can't. Any
attrsclass var gets overwritten by{}, due to what I believe qualifies as a bug inHeavySelect2Mixin, anddata_viewgets overwritten by a bug inModelSelect2Mixin.Steps to Reproduce
attrsanddata_viewclass variable, e.g.:attrsare not applied in the HTML, and thedata_viewis not used to build the select list (the "auto" view is used instead).Expected Behavior
I expected an
attrsanddata_viewclass vars to apply themselves in the same way that anattrsanddata_viewkwarg on the Widget's constructor would, but they instead have no effect.This happens because if there is no
attrsconstructor kwarg,HeavySelect2Mixinexplicitly sendsNoneas theattrskwarg tosuper().__init__(), which eventually gets toWidget.__init__(), which setsself.attrsto{}, overwriting the original class var before it can be used byWidget.get_context(), which sends it toWidget.build_attrs().The
data_viewproblem happens inModelSelect2Mixin.__init__(), where it setsdefaults['data_view']to"django_select2:auto-json", never even trying to read thedata_viewclass var to get the value.This makes it impossible to create canned Widget classes with a custom
attrsordata_viewfor use by multiple different forms, since the only way to specifyattrsanddata_viewis via kwarg.Beta Was this translation helpful? Give feedback.
All reactions