Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#28058 closed Bug (fixed)

Empty Select widget (no choices) evaluates to False

Reported by: László Károlyi Owned by: nobody
Component: Forms Version: 1.11
Severity: Release blocker Keywords: select widget
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by László Károlyi)

Hey guys,

I'm not sure I catched a bug, but I discovered a change between Django==1.10.6 and 1.11.

When you initialize a form with a Select widget, and the select widget has no choices (basically an empty tuple), when evaluating the field with bool(), it will evaluate to False. This causes the django_widget_tweaks module not to render a widget when trying to modify it from within a template, as it uses a bool-like evaluation to check if anything is passed to the set_attr.

code to reproduce:

from django import forms

class MyForm(forms.Form):
    select = forms.ChoiceField(choices=())

x = MyForm()

for item in x:
    print(bool(item))

It will print True with Django==1.10.6, and False with Django==1.11.

The underlying problem is that len() for a Boundfield with no choices will return 0 in the newer version, whereas it returns 1 in the older version.

the template code that fails to render with Django==1.11

{{ widget|set_attr('style:width: 100%') }}

Like I said, I'm not sure if this change is intended, nevertheless I reported it. Please let me know if this is intended, so I can let the creator of django_widget_tweaks know.

Cheers

Change History (5)

comment:1 Changed 2 years ago by László Károlyi

Description: modified (diff)

comment:2 Changed 2 years ago by Tim Graham

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
UI/UX: unset

Bisected to b52c73008a9d67e9ddbb841872dc15cdd3d6ee01 (template-based widget rendering) though I haven't identified the cause of the change.

comment:3 Changed 2 years ago by Tim Graham

Has patch: set

comment:4 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In c09bf8d:

Fixed #28058 -- Restored empty BoundFields evaluating to True.

Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01

comment:5 Changed 2 years ago by Tim Graham <timograham@…>

In 844ae40a:

[1.11.x] Fixed #28058 -- Restored empty BoundFields evaluating to True.

Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01

Backport of c09bf8d76770d39a4d9545b67598cd05adee281b from master

Note: See TracTickets for help on using tickets.
Back to Top