Opened 5 months ago

Closed 4 months ago

#29247 closed Bug (fixed)

A custom blank value in model field choices doesn't work when using named groups

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

Description

While sifting through the source code to understand the internals for a package I'm making, I stumbled upon what looked like a bug.

These lines right here:

named_groups = isinstance(choices[0][1], (list, tuple))
blank_defined = not named_groups and any(choice in ('', None) for choice, __ in choices)

And indeed a bug it was.

The problem was that, had you defined your choices first with a group and then later on a choice that defined the blank value, the current implementation would skip right over it! So you would end up with having both the default blank value and the one you've defined, which doesn't look very pretty.

I've created a pull request on github that fixes the issue here.

Change History (3)

comment:1 Changed 5 months ago by orlnub123

Summary: Fix choices ignoring the defined blank valueChoices ignoring the defined blank value

comment:2 Changed 5 months ago by Tim Graham

Component: FormsDatabase layer (models, ORM)
Patch needs improvement: set
Summary: Choices ignoring the defined blank valueA custom blank value in model field choices doesn't work when using named groups
Triage Stage: UnreviewedAccepted

comment:3 Changed 4 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 2142009:

Fixed #29247 -- Allowed blank model field choice to be defined in nested choices.

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