Opened 8 years ago
Closed 8 years ago
#27239 closed Bug (duplicate)
Unexpected behavior on get_FIELDNAME_display when as int as value
Description ¶
Let's say we have have a choice field called gender and it has intengers as values.
GENDER_CHOICES = ( (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.
Model.instance.create(gender=1) instance.get_gender_display() >>>'1'
The correct response should be
Model.instance.create(gender=1) instance.get_gender_display() >>>'Male'
Change History (3)
follow-up: 2 comment:1 by , 8 years ago
comment:2 by , 8 years ago
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 by , 8 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Duplicate of #20749 which suggests to add a system check to prohibit using incorrect types in choices
.
Why would you use integer values for a CharField in the first place?