#28058: Empty Select widget (no choices) evaluates to False
--------------------------------+--------------------------------------
     Reporter:  László Károlyi  |                    Owner:  nobody
         Type:  Bug             |                   Status:  new
    Component:  Forms           |                  Version:  1.11
     Severity:  Normal          |               Resolution:
     Keywords:  select widget   |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  1
--------------------------------+--------------------------------------
Description changed by László Károlyi:

Old description:

> 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 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

New description:

 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

--

--
Ticket URL: <https://code.djangoproject.com/ticket/28058#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.809ebf6123edf36c3f5f7fa25144ae1b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to