#27239 closed Bug (duplicate)

Unexpected behavior on get_FIELDNAME_display when as int as value

Reported by: Levi Velázquez Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords: choice, field
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Let's say we have have a choice field called gender and it has intengers as values.

        (1, _('Male')),
        (2, _('Female'))

gender = models.CharField(choices=GENDER_CHOICES, null=True)

If we create an instance with a gender as 1 or '1' and then try to display gender field value using get_FIELDNAME_display method, we will get unwanted value. We will get '1' and not 'Male' as it supposed to be.


The correct response should be


Change History (3)

comment:1 Changed 18 months ago by Claude Paroz

Why would you use integer values for a CharField in the first place?

comment:2 in reply to:  1 Changed 18 months ago by Levi Velázquez

Replying to claudep:

Why would you use integer values for a CharField in the first place?

Because it's possible. If Django allows you to assign it, so, it should take care of that case.

comment:3 Changed 18 months ago by Tim Graham

Resolution: duplicate
Status: newclosed

Duplicate of #20749 which suggests to add a system check to prohibit using incorrect types in choices.

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