Code

Opened 5 years ago

Closed 5 years ago

Last modified 3 years ago

#10549 closed (fixed)

BooleanField.formfield() does not handle choices correct. [PATCH]

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

Description

If you have a BooleanField, with choices (as in the example below), the formfield() method always returns a TypedChoicesField, with a blank choice at the beginning. This is because of this entry is included always if the blank is True and BooleanFields are always blank=True. So we have to check for null instead of blank, when using BooleanFields, since the blank flag in the BooleanfField does not specify if the field can be empty in forms in contrast to other fields.

FLAG_CHOICES = (
    (0, _('No'),
    (1, _('Yes'),
)

class Foo(models.Model):
    flag = models.BooleanField(choices=FLAG_CHOICES)

Attachments (1)

fixed-choices-handling-of-BooleanFields.patch (770 bytes) - added by sebastian_noack 5 years ago.

Download all attachments as: .zip

Change History (6)

Changed 5 years ago by sebastian_noack

comment:1 Changed 5 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 5 years ago by ramiro

See also #9640

comment:3 Changed 5 years ago by jacob

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

(In [10500]) Fixed #9640, #10549: BooleanFields with choices, a default, and null=False now correctly doesn't generate a blank option.

comment:4 Changed 5 years ago by SmileyChris

I must say, it is rather amusing how this tests for something which you can't actually do, due to r10456

comment:5 Changed 3 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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.