Opened 4 years ago

Closed 4 years ago

Last modified 4 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: phuihock 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:

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 phuihock 4 years ago.

Download all attachments as: .zip

Change History (6)

Changed 4 years ago by phuihock

comment:1 Changed 4 years ago by julien

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 4 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

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

comment:3 Changed 4 years ago by julien

Yeah, I wasn't thinking clearly here.

comment:4 Changed 4 years ago by phuihock

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

comment:5 Changed 4 years ago by julien

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