Opened 19 years ago
Closed 18 years ago
#2136 closed defect (duplicate)
get_FIELD_display broken under specific conditions
Reported by: | Rudolph Froger | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | normal | Keywords: | |
Cc: | cmlenz@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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 by , 19 years ago
Cc: | added |
---|
comment:2 by , 18 years ago
milestone: | Version 0.93 → Version 1.0 |
---|
comment:3 by , 18 years ago
Component: | Admin interface → 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 by , 18 years ago
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:6 by , 18 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
This appears to be a duplicate of #2122.
0.93 has come and gone.