#15947: get_FIELD_display doesn't work when a given field is an IntegerField
and is
assigned a string value
----------------------+----------------------------------------------
Reporter: phuihock | Owner: nobody
Type: Bug | Status: new
Milestone: | Component: Database layer (models, ORM)
Version: 1.3 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Easy pickings: 0
----------------------+----------------------------------------------
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
--
Ticket URL: <http://code.djangoproject.com/ticket/15947>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.