Opened 10 years ago
Last modified 4 years ago
#23681 closed Cleanup/optimization
NullBooleanSelect should have empty_label or similar — at Initial Version
Reported by: | benjaoming | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Tim Graham | 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
NullBooleanSelect
is responsible of making the values 1, 2, and 3 turn into None, True or False. That's why having custom "choices" kwarg is perhaps a bit over the top. Well, it's pretty meaningless to have a utility class if you start passing a choices kwarg that contains half of the code that makes up NullBooleanSelect.
However, I need one small feature from this widget! Namely, that the following...
class NullBooleanSelect(Select): """ A Select Widget intended to be used with NullBooleanField. """ def __init__(self, attrs=None): choices = (('1', ugettext_lazy('Unknown')), ('2', ugettext_lazy('Yes')), ('3', ugettext_lazy('No'))) super(NullBooleanSelect, self).__init__(attrs, choices)
...is changed to:
class NullBooleanSelect(Select): """ A Select Widget intended to be used with NullBooleanField. """ def __init__(self, empty_label=None, attrs=None): choices = (('1', empty_label or ugettext_lazy('Unknown')), ('2', ugettext_lazy('Yes')), ('3', ugettext_lazy('No'))) super(NullBooleanSelect, self).__init__(attrs, choices)
The motivation is that I often leave out labels to have them put as the default first option of the Select. An example use:
class MyForm(forms.Form): has_payments = forms.NullBooleanField( label="", required=False, widget=NullBooleanSelect(empty_label=_(u"Has previous payments?")) help_text=_(u"Only show subscriptions that have previously been charged"), )
Even more preferable, would be to place the empty_label
kwarg in NullBooleanField
, as that would match the options for ModelChoiceField.