I upgraded Django from 1.8 to 1.11, in the last month and we have now a
bug that the empty choice is always selected on initial page load and
leads to confusion. If I deselect and select the value it work like
intended.
It seems for me that there is no
[https://github.com/django/django/blob/1.11.11/django/forms/widgets.py#L591
check] if something was already selected or if it be a fresh form. Like it
was in
[https://github.com/django/django/blob/1.8.19/django/forms/widgets.py#L523
1.8] in `selected_choices`
I think this report also apply to the `ModelMultipleChoiceField`
e.g.
{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
choices = [(u'', '(no extra)'), (u'cheese', u'Cheese'), (u'Pepper',
u'Pepper')]
extra_topics = forms.MultipleChoiceField(required=False,
choices=choices)
}}}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29273>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* type: Uncategorized => Bug
--
Ticket URL: <https://code.djangoproject.com/ticket/29273#comment:1>
Comment (by Tim Graham):
Can you please be more explicit about the steps to reproduce the issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/29273#comment:2>
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
Comment:
The difference can be seen here:
{{{
from django import forms
choices = [('', '(no extra)'), ('cheese', 'Cheese'), ('Pepper', 'Pepper')]
class Form(forms.Form):
extra_topics = forms.MultipleChoiceField(required=False,
choices=choices)
form = Form()
print(form)
}}}
The first option renders as `<option value="" selected>(no
extra)</option>` in Django 1.11+ but without `selected` in older versions.
--
Ticket URL: <https://code.djangoproject.com/ticket/29273#comment:3>
Comment (by Claude Paroz):
Not surprisingly, this can be bisected to the template-based widget
rendering commit [b52c73008a9d67e9ddbb].
--
Ticket URL: <https://code.djangoproject.com/ticket/29273#comment:4>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/9838 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/29273#comment:5>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"f3b69f9757ec03057441ebbd52b7cdbfed31fb32" f3b69f97]:
{{{
#!CommitTicketReference repository=""
revision="f3b69f9757ec03057441ebbd52b7cdbfed31fb32"
Fixed #29273 -- Prevented initial selection of empty choice in multiple
choice widgets.
Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29273#comment:6>
Comment (by Tim Graham <timograham@…>):
In [changeset:"160829d35b54a97977bf8ce5588aa19b1cfc2b43" 160829d3]:
{{{
#!CommitTicketReference repository=""
revision="160829d35b54a97977bf8ce5588aa19b1cfc2b43"
[2.0.x] Fixed #29273 -- Prevented initial selection of empty choice in
multiple choice widgets.
Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01.
Backport of f3b69f9757ec03057441ebbd52b7cdbfed31fb32 from master.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29273#comment:7>
Comment (by Tim Graham <timograham@…>):
In [changeset:"b25433a225d85c6dace5d5e97f4553c01f035fb1" b25433a]:
{{{
#!CommitTicketReference repository=""
revision="b25433a225d85c6dace5d5e97f4553c01f035fb1"
[1.11.x] Fixed #29273 -- Prevented initial selection of empty choice in
multiple choice widgets.
Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01.
Backport of f3b69f9757ec03057441ebbd52b7cdbfed31fb32 from master.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29273#comment:8>