Opened 9 years ago

Closed 8 years ago

#2136 closed defect (duplicate)

get_FIELD_display broken under specific conditions

Reported by: Rudolph Owned by: adrian
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords:
Cc: cmlenz@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Under some conditions the get_FIELD_display method returns the actual value instead of the human readable value.

A combination of these two situations breaks get_FIELD_display:

  • A choices argument on a IntegerField
  • Changing or adding the object via a manipulator

Example:

M_LIST = ((1, 'Jan'), (2, 'Feb'))
class Test(models.Model):
    month = models.PositiveIntegerField('month', choices=M_LIST)

    def __str__(self):
        return self.get_month_display()

    class Admin:
        pass

Using this model from the shell is no problem, it works as expected. Using is from the admin interface or any other manipulator reveals the problem, __str__ will then return a number (in a string I guess) instead of a string.

I guess the problem is that the value of the field is still a string instead of an integer, when used inside a manipulator.

Change History (6)

comment:1 Changed 9 years ago by Christopher Lenz

  • Cc cmlenz@… added

comment:2 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • milestone changed from Version 0.93 to Version 1.0

0.93 has come and gone.

comment:3 Changed 9 years ago by Thierry Lestavel <dr.virago@…>

  • Component changed from Admin interface to Database wrapper

I just experienced the same issue in my own manipulator's save() method.
To fix it, I cast the data to integer explicitly:

foo= models.Foo(name=new_data['name'],
                severity=int(new_data['severity']),
                type=int(new_data['type']))
# foo.get_severity_display() will print 'critical' and not the numerical value anymore.

I am not sure however if my code is the official practice, or if either the get_FIELD_display or do_html2python methods should be fixed to handle integers instead of strings properly.

By the way, I think this ticket explains the same issue as ticket #2122 (not an advanced user of neither Django nor Trac, so won't take the responsability to put one as duplicate of the other - Adrian?).

comment:4 Changed 9 years ago by Thierry Lestavel <dr.virago@…>

A bit more info about my previous comment:

My manipulator uses SelectFields for new_data['severity'] and new_data['type'].

The way I see it, there is no way for the field to convert_post_html and guess the correct type, as my lists of choices may contain integers, strings... This is not a problem when saving the object or so, but it get trickier with get_FIELD_display.
Could the IntegerField be smart enough to cast its 'value' parameter to int() when summoning get_FIELD_display ?

(We should not expect this method to allow strings anyway...)

comment:5 Changed 8 years ago by anonymous

  • milestone Version 1.0 deleted

Milestone Version 1.0 deleted

comment:6 Changed 8 years ago by Gary Wilson <gary.wilson@…>

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

This appears to be a duplicate of #2122.

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