[Django] #35562: get_FOO_display() method gives choice instead of choice.label for unbound (not saved) model instances with enumeration choices

7 views
Skip to first unread message

Django

unread,
Jun 25, 2024, 3:25:56 PM (4 days ago) Jun 25
to django-...@googlegroups.com
#35562: get_FOO_display() method gives choice instead of choice.label for unbound
(not saved) model instances with enumeration choices
-------------------------------------+-------------------------------------
Reporter: Gerben | Owner: nobody
Morsink |
Type: Bug | Status: new
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{
class FooChoices(models.TextChoices):
BAR = 'BAR', 'bar_with_drinks'

foo = Foo(bar=FooChoices.BAR)

foo.get_bar_display() -> I expect it to return 'bar_with_drinks'
(=FooChoices.BAR.label), but it returns a 'FooChoices.BAR' object.
}}}

This is opposed to when I get Foo out of the database, because then
foo.get_bar_display() will return the FooChoices.BAR.label.
--
Ticket URL: <https://code.djangoproject.com/ticket/35562>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 25, 2024, 3:30:05 PM (4 days ago) Jun 25
to django-...@googlegroups.com
#35562: get_FOO_display() method gives choice instead of choice.label for unbound
(not saved) model instances with enumeration choices
-------------------------------------+-------------------------------------
Reporter: Gerben Morsink | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Gerben Morsink:

Old description:

> {{{
> class FooChoices(models.TextChoices):
> BAR = 'BAR', 'bar_with_drinks'
>
> foo = Foo(bar=FooChoices.BAR)
>
> foo.get_bar_display() -> I expect it to return 'bar_with_drinks'
> (=FooChoices.BAR.label), but it returns a 'FooChoices.BAR' object.
> }}}
>
> This is opposed to when I get Foo out of the database, because then
> foo.get_bar_display() will return the FooChoices.BAR.label.

New description:

{{{
class FooChoices(models.TextChoices):
BAR = 'BAR', 'bar_with_drinks'

foo = Foo(bar=FooChoices.BAR)

foo.get_bar_display() -> I expect it to return 'bar_with_drinks'
(=FooChoices.BAR.label), but it returns a 'FooChoices.BAR' object.
}}}

This is opposed to when I get Foo out of the database, because then
foo.get_bar_display() will return the FooChoices.BAR.label.

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35562#comment:1>

Django

unread,
Jun 25, 2024, 3:30:21 PM (4 days ago) Jun 25
to django-...@googlegroups.com
#35562: get_FOO_display() method gives choice instead of choice.label for unbound
(not saved) model instances with enumeration choices
-------------------------------------+-------------------------------------
Reporter: Gerben Morsink | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Gerben Morsink):

* resolution: => fixed
* status: new => closed

--
Ticket URL: <https://code.djangoproject.com/ticket/35562#comment:2>

Django

unread,
Jun 25, 2024, 3:55:12 PM (4 days ago) Jun 25
to django-...@googlegroups.com
#35562: get_FOO_display() method gives choice instead of choice.label for unbound
(not saved) model instances with enumeration choices
-------------------------------------+-------------------------------------
Reporter: Gerben Morsink | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* resolution: fixed => invalid

Comment:

Hello Gerben Morsink, thanks for your ticket, I understand that you have
solved your issue? In that case the proper resolution here is invalid.
Cheers!
--
Ticket URL: <https://code.djangoproject.com/ticket/35562#comment:3>

Django

unread,
Jun 26, 2024, 12:55:26 AM (4 days ago) Jun 26
to django-...@googlegroups.com
#35562: get_FOO_display() method gives choice instead of choice.label for unbound
(not saved) model instances with enumeration choices
-------------------------------------+-------------------------------------
Reporter: Gerben Morsink | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Gerben Morsink):

Hi Natalia Bidart,
That's correct, I found the issue.

For reference: The problem was that I had CharField where the choices were
a subset of the FooChoices (FooChoicesSubset). When I did set a choice on
the model instance (FooChoices.BAR) that was in FooChoices, but not in
FooChoicesSubset, the answer of get_bar_display() was the FooChoices.BAR
object (which did have a label) on the unbound model instance. I expected
it to return the label, but probably because it was not a "valid" choice
it returned the FooChoices.BAR object. On saving it would have given an
error, but I'm not saving the model instance.

I still think the behaviour could be improved, by either raising an error,
or by showing the label, but I also see that it is a non-normal situation.
--
Ticket URL: <https://code.djangoproject.com/ticket/35562#comment:4>
Reply all
Reply to author
Forward
0 new messages