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!