RelatedFieldWidgetWrapper raises KeyError on Django
See original GitHub issueAffects: Django 1.11.0 but not >=1.11.1 (i never tested <1.10. may affect early 2.0.)
Problem: Trying to create or view a ProductClass in th Dashboard results in a KeyError on context[‘field’].
Bug description
RelatedFieldWidgetWrapper
in apps/dashboard/widgets.py: inherits from Widget but does not call super().__init__.
This means that some variables that Widget.get_context()
builds are not present in RelatedFieldWidgetWrapper.get_context()
(context[‘widget’]).
When rendering ProductClass form in the dashboard, the template calls widget_tweaks’s render_field
with a class attribute added. When adding class attributes, widget tweaks will test the BoundField
for truthyness.
In Django==1.11.0 , BoundField
is only has __len__ (which is used for truthyness tests) which eventually leads to accessing context[‘widget’] (which is not created). In later versions (after this commit ) BoundField got a __bool__()
method so no access to subwidgets happens.
Solution 1: Change RelatedFieldWidgetWrapper
to call super().__init__. I don’t quite understand it entirely and maybe there is a reason it doesn’t, so i’m not submitting a PR
Solution 2: Require >Django==1.11.0
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top GitHub Comments
I’m going to close this for now.
RelatedFieldWidgetWrapper
is intended only for use in the dashboard and uses a template that would never result insubwidgets()
being called. The other issue is resolved in Django 1.11.1, for anyone else that hits it.Honestly, i can’t come up with a use case for
subwidgets()
for this specificRelatedFieldWidgetWrapper
.The only scenario i can imagine is a library (such as widget_tweaks) accessing a method that eventually leads to .subwidgets(). Apart from
Widget.subwidgets()
, these broken methods includeBoundField.subwidgets()
,BoundField.__iter__()
,BoundField.__len__()
, andBoundField.__getitem__()
.If
RelatedFieldWidgetWrapper
(and inside a BoundField) is safe from such a scenario, then this bug should be closed.