#28502 closed Bug (fixed)
Select widget doesn't accept non-string as value
Reported by: | Nguyễn Hồng Quân | Owned by: | Srinivas Reddy Thatiparthy |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | widget filter |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I made a model, with age_bracket
field, which accepts values from choices like this:
AGE_BRACKET = ( # The value in the first column will be saved in Postgres Range field, # so the upper boundary is not counted, i.e for (18, 25) range, the value 25 # is not counted. ((None, 18), _('Under 18')), ((18, 25), _('18 - 24')), ((25, 36), _('25 - 35')), ((36, 46), _('36 - 45')), ((46, 56), _('46 - 55')), ((46, 66), _('56 - 65')), ((66, None), _('Above 65')), )
However, when being rendered as form, all values become empty string. This happens since Django 1.11. In Django 1.10, it was OK.
I found the cause is the widget template select_option.html
<option value="{{ widget.value|stringformat:'s' }}"{% include "django/forms/widgets/attrs.html" %}>{{ widget.label }}</option>
The stringformat
filter turns every non-string value to empty string.
My suggestion is that, update stringformat
so that it returns str(value)
if value is not a string. Is it right way to do?
Change History (10)
comment:1 by , 7 years ago
Component: | Forms → Template system |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 1.11 → master |
comment:2 by , 7 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:4 by , 7 years ago
Has patch: | set |
---|---|
Needs tests: | set |
comment:5 by , 7 years ago
Needs tests: | unset |
---|
comment:7 by , 7 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Note:
See TracTickets
for help on using tickets.
I also think that
stringformat
should be improved to accept tuples as its basevalue
. In Python the printf syntax special cases tuples as it considers them as a list of placeholder values. This does not make sense instringformat
as the string containing the placeholder is always a single placeholder.