Not using NullBooleanField¶
A BooleanField
in Django accepts only the two values: true
and false
. If you need to accept NULL
values, you have to use a NullBooleanField
.
Anti-pattern¶
The following model uses a BooleanField
with the option null=True
, instead of using a NullBooleanField
.
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
activated = models.BooleanField(null=True)
Best practice¶
Use a NullBooleanField
instead:
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
# Using NullBooleanField instead
activated = models.NullBooleanField()