Opened 13 years ago

Closed 12 years ago

Last modified 10 years ago

#2855 closed defect (wontfix)

[patch] BooleanField should use False as default (unless provided)

Reported by: Chris Beaven Owned by: nobody
Component: Database layer (models, ORM) Version:
Severity: normal Keywords: BooleanField NullBooleanField
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When creating models, BooleanFields which haven't explicitly been given a default have to be specifically set or a SQL error will occur (because the default value will be ). Simple solution is to force the default to False if no default is given.

# The model
class TestModel(models.Model):
    check = models.BooleanField()

# then in code..
t = TestModel()  # will error, because the boolean field will return the default of '' instead of False

Attachments (1)

boolean_default.patch (513 bytes) - added by Chris Beaven 13 years ago.
simple fix

Download all attachments as: .zip

Change History (9)

Changed 13 years ago by Chris Beaven

Attachment: boolean_default.patch added

simple fix

comment:1 Changed 13 years ago by James Bennett

I'm not sure I understand the bug here; any type of field should throw an error if it doesn't have null=True and you don't supply a value.

comment:2 Changed 13 years ago by Chris Beaven

null=True doesn't make sense to me for BooleanFields because this are only two states it can be in - I'm not wanting a NullBooleanField. What does make sense is that if the default hasn't been specified as True for a boolean, you assume False.

Here are the local vars for the SQL error raised:

params = ('',)
sql	'INSERT INTO "project.test_model" ("check") VALUES (%s)'

comment:3 Changed 13 years ago by Chris Beaven

a related ticket: #2890

comment:4 Changed 13 years ago by Bastian Kleineidam <calvin@…>

You need the null=True to check if the value has been set or not. So BooleanField is actually a triple-state variable: True, False and unset.

If null=True should be the default case is of course highly application dependant. In my applications so far I always used null=True, but your mileage my vary.

comment:5 Changed 13 years ago by Chris Beaven

Bastian, in Django there are two similar fields: BooleanField and NullBooleanField.

BooleanField can only have one of two states: True or False.

comment:6 Changed 13 years ago by Simon G. <dev@…>

Keywords: BooleanField NullBooleanField added
Triage Stage: UnreviewedDesign decision needed

comment:7 Changed 12 years ago by Adrian Holovaty

Resolution: wontfix
Status: newclosed

Closing this as wontfix for the reasons pointed out in the above comments.

comment:8 Changed 10 years ago by rlaager@…

For the record, this was fixed at some point.

Note: See TracTickets for help on using tickets.
Back to Top