#29273 closed Bug (fixed)
MultipleChoiceField incorrectly selects the empty value for an empty form
| Reported by: | Tim Kleinschmidt | Owned by: | nobody |
|---|---|---|---|
| Component: | Forms | Version: | 1.11 |
| Severity: | Release blocker | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
hi,
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 check if something was already selected or if it be a fresh form. Like it was in 1.8 in selected_choices
I think this report also apply to the ModelMultipleChoiceField
e.g.
Code highlighting:
choices = [(u'', '(no extra)'), (u'cheese', u'Cheese'), (u'Pepper', u'Pepper')] extra_topics = forms.MultipleChoiceField(required=False, choices=choices)
Change History (8)
comment:1 by , 8 years ago
| Type: | Uncategorized → Bug |
|---|
comment:2 by , 8 years ago
comment:3 by , 8 years ago
| Severity: | Normal → Release blocker |
|---|---|
| Summary: | MultipleChoiceField auto selects on initial load the empty value → MultipleChoiceField incorrectly selects the empty value for an empty form |
| Triage Stage: | Unreviewed → Accepted |
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.
comment:4 by , 8 years ago
Not surprisingly, this can be bisected to the template-based widget rendering commit [b52c73008a9d67e9ddbb].
Can you please be more explicit about the steps to reproduce the issue?