Opened 10 years ago

Last modified 4 years ago

#23681 closed Cleanup/optimization

NullBooleanSelect should have choices kwarg — at Version 1

Reported by: benjaoming Owned by: benjaoming
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 (last modified by benjaoming)

NullBooleanSelect is responsible of making the values 1, 2, and 3 turn into None, True or False. That's very nice of it, however it does not allow to customize the texts of the choices.

I'm not sure if exposing the internal 1, 2, 3 representation is a good idea, but it would seem okay since it follows the convention of other Select widgets. Ideally, I would like to see this...

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)

...changed to:

class NullBooleanSelect(Select):
    """
    A Select Widget intended to be used with NullBooleanField.
    """
    def __init__(self, choices=None, attrs=None):
        if not choices:
            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):
    gender = forms.NullBooleanField(
        label="",
        required=False,
        widget=NullBooleanSelect(choices=[("1", "Male and female"),
                                          ("2", "Only female"),
                                          ("3", "Only Male")])
        help_text=_(u"Only show subscriptions that have previously been charged"),
    )

Even more preferable, would be to place the choices kwarg in NullBooleanField, as that would match the options for ChoiceField.

<b>Updated</b> In the original issue report, I put empty_label but realized that when selecting "Yes", it was impossible for the user to see what "Yes" was the answer to.

Change History (1)

comment:1 by benjaoming, 10 years ago

Description: modified (diff)
Owner: changed from nobody to benjaoming
Status: newassigned
Summary: NullBooleanSelect should have empty_label or similarNullBooleanSelect should have choices kwarg
Note: See TracTickets for help on using tickets.
Back to Top