Listen as audio |

Hello Techies,
In this blog, we will check different NullBooleanSelect Django form fields (NullBooleanField) with examples.
The fields of the form are classes themselves; They manage form data and perform validation when a form is submitted.
Syntax : FieldType(**kwargs)
Table of Contents
Different types of NullBooleanSelect Django form fields
NullBooleanField
The default widget for this input is the NullBooleanSelect. It does not validate anything so it never increases the ValidationError. The empty value is None. It normalizes to a Python True, False, and None value.
Syntax: NullBooleanField(**kwargs)
Example:
from django import forms
class NullBooleanForm(forms.Form):
null_boolean_field= forms.NullBooleanField(label='Gender', label_suffix = " : ",
required = True, disabled = False,
widget=forms.NullBooleanSelect(attrs={'class': 'form-control'}),
error_messages = {'required':"This field is required."})
Let’s run the server and check the result.
Rendered HTML
Explanation:
Field Options | Description |
---|---|
label | The label argument allows you to specify a “Field Caption” label for this area. |
label_suffix | The label_suffix argument allows you to insert some text after the field’s label. |
required | By default, each field assumes a value as required, so you need to set required = false to make it not necessary. |
widget | Widget Argument lets you specify widget classes to use when presenting this field. So you can use bootstrap class or other CSS class for this field to decorate your field. Also, you can pass the choice list. For eg. null_boolean_field= forms.NullBooleanField(widget=forms.NullBooleanSelect(attrs={‘class’: ‘form-control’})) |
error_messages | error_messages argument allows you to overwrite the default error message that the field will raise. Pass the error message you want to overwrite in the dictionary with matching keys. |
disabled | Disabled Boolean Argument, when set to true, disables the form field using the disabled HTML attribute so that it cannot be edited by users. |
I hope you understand all the NullBooleanSelect Django form fields. For more details, you can check out the official site.
Find the source code for NullBooleanSelect Django form fields Github link at:
https://github.com/pranalikambli/django_form_fields
