#5563 closed (fixed)
BooleanField should raise an error if null=True
| Reported by: | Owned by: | Sam Bull | |
|---|---|---|---|
| Component: | Core (Serialization) | Version: | dev | 
| Severity: | Keywords: | ||
| Cc: | shaun@…, David Larlet | Triage Stage: | Design decision needed | 
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
django.db.models.fields.BooleanField.to_python raises an error on "None" input, but this is valid if null=True for the field.
Attachments (2)
Change History (14)
by , 18 years ago
| Attachment: | booleanfield-to-python-can-be-none.patch added | 
|---|
comment:1 by , 18 years ago
| Cc: | added | 
|---|
comment:3 by , 18 years ago
| Has patch: | unset | 
|---|---|
| Summary: | BooleanField can be None if null=True → BooleanField should raise an error if null=True | 
um... well, how about that: I never noticed NullBooleanField before. I'd close this, but I'll leave it up to others to decide if the following should be done so others don't go astray:
1) doc for BooleanField should mention that one should use NullBooleanField rather than null=True.
2) passing null=True to BooleanField init should cause an exception to be raised (whose error text says: "use NullBooleanField")
comment:4 by , 18 years ago
| Triage Stage: | Unreviewed → Design decision needed | 
|---|
There's been a lot of discussion lately about BooleanField and NullBooleanField, and it's debatable whether NullBooleanField is really appropriate for this use case.
comment:6 by , 17 years ago
| Cc: | added | 
|---|---|
| Has patch: | set | 
I can confirm, it breaks json deserialization with None values.
== True from the patch is probably useless?
comment:7 by , 17 years ago
| milestone: | → 1.1 | 
|---|---|
| Triage Stage: | Design decision needed → Accepted | 
We either need to fix this or raise a validation warning about BooleanField(null=True).
comment:8 by , 17 years ago
| Owner: | changed from to | 
|---|---|
| Status: | new → assigned | 
by , 17 years ago
| Attachment: | boolean_no_null_validation.patch added | 
|---|
patch to raise a validation error for BooleanField(null=True)
comment:9 by , 17 years ago
I've added a new patch. This adds a model validation check that raises an error if null=True is set within a BooleanField.
I also added a test for the new behaviour and updated a serialization test that was failing after the change. 
comment:10 by , 17 years ago
| Triage Stage: | Accepted → Design decision needed | 
|---|
comment:11 by , 17 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | assigned → closed | 
(patch to fix django.db.models.fields.init.py