question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RelatedFieldWidgetWrapper raises KeyError on Django

See original GitHub issue

Affects: 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
solarissmokecommented, May 23, 2018

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 in subwidgets() being called. The other issue is resolved in Django 1.11.1, for anyone else that hits it.

0reactions
kmetaxascommented, May 21, 2018

Honestly, i can’t come up with a use case for subwidgets() for this specific RelatedFieldWidgetWrapper.

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 include BoundField.subwidgets() , BoundField.__iter__(), BoundField.__len__(), and BoundField.__getitem__().

If RelatedFieldWidgetWrapper (and inside a BoundField) is safe from such a scenario, then this bug should be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django, prefetch related raises key error - Stack Overflow
I have a query which executes a reverse prefetch related. But raises the following error: KeyError at /api/group/ (1,).
Read more >
Issue #44 · jazzband/django-widget-tweaks - GitHub
The incriminated line: https://github.com/kmike/django-widget-tweaks/blob/master/ ... RelatedFieldWidgetWrapper raises KeyError on Django ...
Read more >
Source code for django.contrib.admin.options
RelatedFieldWidgetWrapper ( formfield.widget, db_field.remote_field, self.admin_site, ... **defaults) except FieldError as e: raise FieldError( '%s.
Read more >
Is this a bug? KeyError: 'is_popup' on mysite/admin, while ...
KeyError : 'is_popup' on mysite/admin, while logging level = DEBUG. ... I am a django newbie, just making my first django site. ......
Read more >
django.utils.safestring mark_safe Example Code
append(params_dict) except KeyError: continue return mark_safe( render_to_string( "debug_toolbar/panels/sql_stacktrace.html", { "stacktrace": stacktrace, " ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found