Django ModelForm doesn't seem to check BooleanField

I am using django 1.5.4

Here is a minimal example of the problem I am facing.

Model:

#models.py
from django.db import models

class SampleModel(models.Model):
    spam = models.BooleanField()

The form:

#forms.py
from django.forms import ModelForm
from .models import SampleModel

class SampleModelForm(ModelForm):
    class Meta:
        model = SampleModel
        fields = ('spam', )

From the django shell:

>>> data = {} #intentionally blank
>>> form = SampleModelForm(data)
>>> is_valid = form.is_valid() #is_valid is True
>>> form.save() # model instance is created with "spam" set to False by default.

Am I checking the form incorrectly? form.is_validcorrectly checks fields of other types. docs indicate that by default all fields are required, but is_validreturns Truewithout the presence of a boolean field.

I need to make sure that a boolean field is present in the input. At the moment, I manually check if the field is present and has a type bool. Do you think I should override form.is_validand add this check so that it can be reused for other models?

+4
1

( , ), BooleanField blank=True __init__, . (False , BooleanField , ), , .

, True, - required True - ( __init__). True False, Python None, .

, BooleanField - . , submit False. , . (, RadioSelect), , , - False, , BooleanField to_python boolean , .

, False , , , required=True .

+7

All Articles