#27445: RadioSelect widget does not work for NullBooleanField
-------------------------------------+-------------------------------------
Reporter: Anne Fleischer | Owner: Jacob
| Rief
Type: New feature | Status: assigned
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: NullBooleanField, | Triage Stage: Accepted
RadioSelect |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Takis Issaris):
After digging through the code for quite some time, I decided to cheat a
little and have another go at search StackOverflow.
This is what I found regarding the saved value not being reflected in the
rendered form (in an UpdateView for example).
It was suggested [http://stackoverflow.com/a/28262878] that one should use
the following choices to make it work:
{{{
choices=((None, 'N/A'), (True, 'Yes'), (False, 'No'))
}}}
That works for me for True and False, but not for the None value of the
NullBooleanField.
On the same thread a user named 'x0nix' suggests this snippet
[http://stackoverflow.com/a/28906939]:
{{{
class NullBooleanRadioSelect(RadioSelect):
def __init__(self, *args, **kwargs):
choices = (
(None, ugettext_lazy('Unknown')),
(True, ugettext_lazy('Yes')),
(False, ugettext_lazy('No'))
)
super(NullBooleanRadioSelect, self).__init__(choices=choices,
*args, **kwargs)
_empty_value = None
}}}
{{{
...
widgets = {'foo': NullBooleanRadioSelect()}
}}}
With this snippet all three values seem to be rendered correctly.
--
Ticket URL: <https://code.djangoproject.com/ticket/27445#comment:6>
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.e358eff6c5fa2660384c41529ede1444%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.