Changes between Initial Version and Version 1 of Ticket #23681


Ignore:
Timestamp:
Oct 18, 2014, 10:24:02 AM (10 years ago)
Author:
benjaoming
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #23681

    • Property Owner changed from nobody to benjaoming
    • Property Status newassigned
    • Property Summary NullBooleanSelect should have empty_label or similarNullBooleanSelect should have choices kwarg
  • Ticket #23681 – Description

    initial v1  
    1 `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.
     1`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.
    22
    3 However, I need one small feature from this widget! Namely, that the following...
     3I'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...
    44
    55
     
    1717
    1818
    19 ...is changed to:
     19...changed to:
    2020
    2121
     
    2525    A Select Widget intended to be used with NullBooleanField.
    2626    """
    27     def __init__(self, empty_label=None, attrs=None):
    28         choices = (('1', empty_label or ugettext_lazy('Unknown')),
    29                    ('2', ugettext_lazy('Yes')),
    30                    ('3', ugettext_lazy('No')))
     27    def __init__(self, choices=None, attrs=None):
     28        if not choices:
     29            choices = (('1', empty_label or ugettext_lazy('Unknown')),
     30                       ('2', ugettext_lazy('Yes')),
     31                       ('3', ugettext_lazy('No')))
    3132        super(NullBooleanSelect, self).__init__(attrs, choices)
    3233}}}
     
    3536The motivation is that I often leave out labels to have them put as the default first option of the Select. An example use:
    3637
    37 
    3838{{{
    3939class MyForm(forms.Form):
    40     has_payments = forms.NullBooleanField(
     40    gender = forms.NullBooleanField(
    4141        label="",
    4242        required=False,
    43         widget=NullBooleanSelect(empty_label=_(u"Has previous payments?"))
     43        widget=NullBooleanSelect(choices=[("1", "Male and female"),
     44                                          ("2", "Only female"),
     45                                          ("3", "Only Male")])
    4446        help_text=_(u"Only show subscriptions that have previously been charged"),
    4547    )
     
    4749}}}
    4850
    49 Even more preferable, would be to place the `empty_label` kwarg in `NullBooleanField`, as that would match the options for ModelChoiceField.
     51Even more preferable, would be to place the `choices` kwarg in `NullBooleanField`, as that would match the options for ChoiceField.
     52
     53<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.
Back to Top