Opened 17 years ago
Closed 17 years ago
#7913 closed (fixed)
Backwards incompatible change by #4412 / r7977: get_FIELD_display() returns blank value
| Reported by: | miracle2k | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Keywords: | ||
| Cc: | evenson@… | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Previously, get_FIELD_display(), for a blank or unknown value of FIELD, return the value unchanged, e.g. say None or an empty string.
After the change, it returns the automatically inserted blank choice (-------). This is due to it now using field.flatchoices, which is dynamically generated through the get_choices() method, whereas it previously just used field.choices, which contains the unmodified tuple the field receives through init.
This may also be an unwanted discrepancy between the two attributes.
class Whiz(models.Model):
CHOICES = (
(1,'First'),
(0,'Other'),
)
c = models.IntegerField(choices=CHOICES, null=True)
>>> w = Whiz(c='')
>>> w.save()
>>> w.get_c_display()
u''
Expected:
u''
Got:
u'---------'
Attachments (1)
Change History (7)
by , 17 years ago
comment:1 by , 17 years ago
comment:2 by , 17 years ago
| Has patch: | set |
|---|
comment:3 by , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
comment:4 by , 17 years ago
| Resolution: | fixed |
|---|---|
| Status: | closed → reopened |
Getting an error in get_flatchoices():
File "/home/ese/SVN_PROJECTS/django/django/db/models/fields/__init__.py", line 388, in get_flatchoices
return first_choices + list(self.flatchoices)
NameError: global name 'first_choices' is not defined
Looks like a typo in:
def get_flatchoices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
"Returns flattened choices with a default blank choice included."
first_choice = include_blank and blank_choice or []
return first_choices + list(self.flatchoices)
believe this should be:
def get_flatchoices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
"Returns flattened choices with a default blank choice included."
first_choice = include_blank and blank_choice or []
return first_choice + list(self.flatchoices)
comment:5 by , 17 years ago
| Cc: | added |
|---|
comment:6 by , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | reopened → closed |
The whole choices implementation is slightly confusing. Field currently has the following defined:
The patch changes this to (changes marked with !):