In the docs, the nullbooleanfield is represented as a <select> box with "Unknown", "Yes" and "No" choices. How can I change the values of select to some other more meaningful texts and map it back to the yes,no and unknown values in my modelform?

For example I have yes_no_required = models.NullBooleanField() and I would like to have 'yes I acknowledge this', 'no, I do not like this' and 'I do not know now' mapping to yes, no and required accordingly.

Simple approach would be to change widget choices in your form:

class YourForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(YourForm, self).__init__(*args, **kwargs)
        new_choices = ((1, 'I do not know now'),
                       (2, 'Yes I acknowledge this'),
                       (3, 'No, I do not like this'))
        self.fields['your_field'].widget.choices = new_choices

You can handle this in a form (withouth modifying the models) like this:

forms.NullBooleanField(
    widget=forms.RadioSelect(renderer=HorizontalRadioRendererSpace,
        choices=CHOICES),
    label='...',
    required=False)

I think it is a bad idea to use NullBooleanField for data which can be in three states like: yes/no/unset.

Editing the data works. But the big trouble arises if you want to write a search form for this data.

Then the "empty" values can have two meanings:

  • Don't care for this attribute. Include all rows
  • Include only these rows, where the column is NULL.

The solution is simple: Use a ChoiceField.

More details here: https://github.com/guettli/programming-guidelines/blob/master/README.rst

You can modify your field widget in your ModelForm meta class:

CHOICES = (
            (None, "I do not know now"),
            (True, "Yes I acknowledge this"),
            (False, "No, I do not like this"),
           )
class DemoForm(forms.ModelForm):
    class Meta:
        model = Demo
        fields = ("demo_field",)
        widgets = {
              'demo_field': forms.RadioSelect(choices = CHOICES),
                  }

I spent half an hour putting together a demo just to prove to myself this works:

CHOICES = (
    (None, "I do not know now"),
    (True, "Yes I acknowledge this"),
    (False, "No, I do not like this")
)

class Demo(models.Model):
    yes_no_required = models.NullBooleanField(choices = CHOICES)

class DemoForm(forms.ModelForm):
    class Meta:
        model = Demo

I looked in the Django source code. Everything can accept a choices array. Obviously, for some of those that doesn't make sense, but it's possible.

  • 3
    Seems like Django adds yet another choice for Null to the FormField choices, as the null-attr is set true. You'd need to pass the choices on to the FormField as well, to actually limit it to these choices and not an additional dashed null choice. – codingjoe Aug 11 '14 at 13:22
  • This doesn't appear to work anymore. TypeError: __init__() got an unexpected keyword argument 'choices' – Nathan Osman Aug 4 '17 at 18:49

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.