{{{
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'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27239>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
Why would you use integer values for a CharField in the first place?
--
Ticket URL: <https://code.djangoproject.com/ticket/27239#comment:1>
Comment (by levivm):
Replying to [comment:1 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/27239#comment:2>
* status: new => closed
* resolution: => duplicate
Comment:
Duplicate of #20749 which suggests to add a system check to prohibit using
incorrect types in `choices`.
--
Ticket URL: <https://code.djangoproject.com/ticket/27239#comment:3>