Opened 18 years ago

Closed 17 years ago

Last modified 15 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

Description

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()
t.save()  # 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 18 years ago.
simple fix

Download all attachments as: .zip

Change History (9)

by Chris Beaven, 18 years ago

Attachment: boolean_default.patch added

simple fix

comment:1 by James Bennett, 18 years ago

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 by Chris Beaven, 18 years ago

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 by Chris Beaven, 18 years ago

a related ticket: #2890

comment:4 by Bastian Kleineidam <calvin@…>, 18 years ago

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 by Chris Beaven, 18 years ago

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

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

comment:6 by Simon G. <dev@…>, 18 years ago

Keywords: BooleanField NullBooleanField added
Triage Stage: UnreviewedDesign decision needed

comment:7 by Adrian Holovaty, 17 years ago

Resolution: wontfix
Status: newclosed

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

comment:8 by rlaager@…, 15 years ago

For the record, this was fixed at some point.

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