Code

Opened 8 years ago

Closed 7 years ago

Last modified 5 years ago

#2855 closed defect (wontfix)

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

Reported by: SmileyChris 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: UI/UX:

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 SmileyChris 8 years ago.
simple fix

Download all attachments as: .zip

Change History (9)

Changed 8 years ago by SmileyChris

simple fix

comment:1 Changed 8 years ago by ubernostrum

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 8 years ago by SmileyChris

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 8 years ago by SmileyChris

a related ticket: #2890

comment:4 Changed 8 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 8 years ago by SmileyChris

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 7 years ago by Simon G. <dev@…>

  • Keywords BooleanField NullBooleanField added
  • Triage Stage changed from Unreviewed to Design decision needed

comment:7 Changed 7 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

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

comment:8 Changed 5 years ago by rlaager@…

For the record, this was fixed at some point.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.