﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
15947	get_FIELD_display doesn't work when a given field is an IntegerField and is assigned a string value	Phui Hock	nobody	"Given a model class:
{{{
class TestModel(models.Model):
    RATE_CHOICES = (
        (1, _('Poor')),
        (2, _('Neutral')),
        (3, _('Good')),
    )
    rate = models.IntegerField(choices=RATE_CHOICES, default=1)
}}}

get_rate_display fails to return the choice display value if the IntegerField is assigned a string value. 
{{{
In [2]: from test.models import TestModel
In [3]: a = TestModel()
In [4]: a.get_rate_display()
Out[4]: u'Poor'
In [5]: a.rate = 2
In [6]: a.get_rate_display()
Out[6]: u'Neutral'
In [7]: a.rate = '2'
In [8]: a.get_rate_display()
Out[8]: u'2'  # problem!
}}}

Since the keys of the field's flatchoices are integers, get_FIELD_display will fail to retrieve the corresponding display value, so it returns the value as it is.

While this is obviously a user error, the subtlety of the problem is confusing at best. Ideally, get_FIELD_display should cast the value to the type the field represents before looking up the display value from the dictionary object.

This is related to http://code.djangoproject.com/ticket/2136"	Bug	closed	Database layer (models, ORM)	1.3	Normal	wontfix			Accepted	1	0	1	0	0	0
