Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#15947 closed Bug (wontfix)

get_FIELD_display doesn't work when a given field is an IntegerField and is assigned a string value

Reported by: Phui Hock Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Attachments (1)

base.diff (561 bytes ) - added by Phui Hock 13 years ago.

Download all attachments as: .zip

Change History (6)

by Phui Hock, 13 years ago

Attachment: base.diff added

comment:1 by Julien Phalip, 13 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 by Jacob, 13 years ago

Resolution: wontfix
Status: newclosed

I really disagree - implicit casts are incredibly un-Pythonic.

comment:3 by Julien Phalip, 13 years ago

Yeah, I wasn't thinking clearly here.

comment:4 by Phui Hock, 13 years ago

But, isn't Django already doing that with get_db_prep_value?

comment:5 by Julien Phalip, 13 years ago

Django does cast values but only at a few key stages, for example when receiving data from a form (since browsers have different ways of formatting data) or before saving to the database (i.e. with get_db_prep_value). However, if you are manually and directly feeding improper data to a field, as illustrated in your code sample above, then you're on your own. It is your responsibility to provide sensibly formatted data as long as it is within your control -- that is, be pythonic. If you find yourself stuck because improper data is passed to a field in a way that is outside of your control, then feel free to bring the question up on the dev-list.

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