Opened 4 years ago

Last modified 4 years ago

#31124 closed Bug

Model.get_FOO_display() does not work correctly with inherited choices. — at Version 4

Reported by: Yash Jhunjhunwala Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Release blocker Keywords:
Cc: Carlton Gibson, Sergey Fedoseev Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mariusz Felisiak)

Given a base model with choices A containing 3 tuples
Child Model inherits the base model overrides the choices A and adds 2 more tuples
get_foo_display does not work correctly for the new tuples added

Example:

class A(models.Model):
   foo_choice = [("A","output1"),("B","output2")]
   field_foo = models.CharField(max_length=254,choices=foo_choice)
   class Meta:
       abstract = True

class B(A):
   foo_choice = [("A","output1"),("B","output2"),("C","output3")]
   field_foo = models.CharField(max_length=254,choices=foo_choice)

Upon invoking get_field_foo_display() on instance of B ,
For value "A" and "B" the output works correctly i.e. returns "output1" / "output2"
but for value "C" the method returns "C" and not "output3" which is the expected behaviour

Change History (4)

comment:1 by Mariusz Felisiak, 4 years ago

Component: UncategorizedDatabase layer (models, ORM)
Easy pickings: unset
Resolution: needsinfo
Status: newclosed
Summary: get foo display - model inheritance does not work correctlyModel.get_FOO_display() does not work correctly with inherited choices.

Thanks for this report. Can you provide models and describe expected behavior? Can you also check if it's not a duplicate of #30931?, that was fixed in Django 2.2.7.

in reply to:  1 ; comment:2 by Yash Jhunjhunwala, 4 years ago

Description: modified (diff)

Replying to felixxm:

Thanks for this report. Can you provide models and describe expected behavior? Can you also check if it's not a duplicate of #30931?, that was fixed in Django 2.2.7.

in reply to:  2 comment:3 by Yash Jhunjhunwala, 4 years ago

Added the models and expected behaviour. It is not a duplicate of #30931. Using Django 2.2.9

Replying to felixxm:

Thanks for this report. Can you provide models and describe expected behavior? Can you also check if it's not a duplicate of #30931?, that was fixed in Django 2.2.7.

comment:4 by Mariusz Felisiak, 4 years ago

Cc: Carlton Gibson Sergey Fedoseev added
Description: modified (diff)
Resolution: needsinfo
Status: closednew
Triage Stage: UnreviewedAccepted
Version: 2.23.0

Thanks for an extra info. I was able to reproduce this issue, e.g.

>>> B.objects.create(field_foo='A').get_field_foo_display()
output1
>>> B.objects.create(field_foo='B').get_field_foo_display()
output2
>>> B.objects.create(field_foo='C').get_field_foo_display()
C

Regression in 2d38eb0ab9f78d68c083a5b78b1eca39027b279a (Django 2.2.7).

Last edited 4 years ago by Mariusz Felisiak (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top