Opened 6 years ago

Closed 2 months 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

Description

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

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

vs.

foo = models.BooleanField(default=False)

Attachments (1)

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

Download all attachments as: .zip

Change History (11)

comment:1 Changed 6 years ago by alex@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 6 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 6 years ago by thejaswi_puthraya

  • Component changed from Uncategorized to Database layer (models, ORM)

comment:4 Changed 6 years ago by SmileyChris

  • Triage Stage changed from Unreviewed to Accepted

Changed 6 years ago by SafPlusPlus <freark@…>

Regression test for #10928

comment:5 Changed 6 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 4 years ago by lukeplant

  • Severity set to Normal
  • Type set to Bug

comment:7 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:8 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:10 Changed 2 months ago by timgraham

  • Resolution set to fixed
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top