Changes between Version 1 and Version 3 of Ticket #23681


Ignore:
Timestamp:
Oct 23, 2014, 3:31:30 PM (10 years ago)
Author:
benjaoming
Comment:

Hi timgraham! I think what you are hinting at is this:

class MyForm(forms.Form):
    gender = forms.NullBooleanField(
        label="",
        required=False,
        widget=Select(choices=[(None, "Male and female"),
                               (True, "Only female"),
                               (False, "Only Male")])
        help_text="Choose gender",
    )

It will work, but not perfectly. NullBooleanSelect has special methods render, value_from_datadict that are tailored for NullBooleanField (I'm not sure why _has_changed has gone, it used to also be customized). As I understand, they are there to ensure that the None value can be deliberately extracted from the value '1'.

I'm totally open for suggestions... I would like to be able to achieve custom labels in the choices of NullBooleanField, because I think it's an essential option that can keep us from creating one-trick sub classes. And I would probably often like to use other words than Unknown, Yes, and No.

Seeing that NullBooleanField always returns None, False, and True, it might make sense to put them as explicit kwargs, like how empty_label is used. This is perhaps better than using the widget....

class MyForm(forms.Form):
    gender = forms.NullBooleanField(
        label="",
        required=False,
        empty_label="Male and female",
        true_label="Only female",
        false_label="Only Male",
        help_text="Choose gender",
    )

To me, that seems nice, clean, explicit, and useful :) And yes, I can write a patch!

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #23681 – Description

    v1 v3  
    4444                                          ("2", "Only female"),
    4545                                          ("3", "Only Male")])
    46         help_text=_(u"Only show subscriptions that have previously been charged"),
     46        help_text="Choose gender",
    4747    )
    4848
Back to Top