Opened 9 years ago

Closed 3 years ago

#10928 closed Bug (fixed)

BooleanField: different behaviour when default=callable

Reported by: alex@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When using a callable, the behaviour of the BooleanField is different from just using a constant. Example:

foo = models.BooleanField(default=lambda: False)


foo = models.BooleanField(default=False)

Attachments (1)

10928-r11178.diff (2.0 KB) - added by SafPlusPlus <freark@…> 9 years ago.
Regression test for #10928

Download all attachments as: .zip

Change History (11)

comment:1 Changed 9 years ago by alex@…

Oh, hit submit too fast. Basically, the callable-form emits, when used in a ModelForm (for example the admin), the following hidden initial-field:

<input type="hidden" name="initial-child1_set-0-foo" value="False" id="id_child1_set-0-foo" />

This has the effect that form.has_changed() is always True, so that extra forms of formsets are always considered for validation.

comment:2 Changed 9 years ago by alex@…

Ok, I found out that I can work around by returning None instead of False from the callable, the value="" attribute is left out in the hidden initial. A possible fix would be to make the CheckboxInput widget accept u"False" as False in the _has_changed check, or by preventing the hidden initial field from having value="False" in the first place.

comment:3 Changed 9 years ago by Thejaswi Puthraya

Component: UncategorizedDatabase layer (models, ORM)

comment:4 Changed 9 years ago by Chris Beaven

Triage Stage: UnreviewedAccepted

Changed 9 years ago by SafPlusPlus <freark@…>

Attachment: 10928-r11178.diff added

Regression test for #10928

comment:5 Changed 9 years ago by SafPlusPlus <freark@…>

Added a test, which is hopefully correct. Maybe I should've named the file in way that more reflects that it only contains a test and not a fix, but then again, I'm just a newbie so I may get away with it. :)

comment:6 Changed 7 years ago by Luke Plant

Severity: Normal
Type: Bug

comment:7 Changed 6 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 Changed 6 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:10 Changed 3 years ago by Tim Graham

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top