Opened 16 years ago

Closed 16 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'---------'

Reference:
http://groups.google.com/group/django-developers/browse_thread/thread/5b899cb2d8388717/50ed30aa358e2d59?hl=en#50ed30aa358e2d59

Attachments (1)

7913.diff (2.5 KB ) - added by miracle2k 16 years ago.

Download all attachments as: .zip

Change History (7)

by miracle2k, 16 years ago

Attachment: 7913.diff added

comment:1 by miracle2k, 16 years ago

The whole choices implementation is slightly confusing. Field currently has the following defined:

property  choices                -> returns what was passed to __init__ (_choices); no blank value
method    get_choices            -> returns choices + blank value
method    get_choices_default    -> simply an alias for get_choices?, used only a few times, mostly involving oldforms code
property  flatchoices            -> returns flat version of get_choices (thus includes the blank value)

The patch changes this to (changes marked with !):

  property  choices                -> returns what was passed to __init__ (_choices)
! property  flatchoices            -> returns flat version of choices (now not including a blank value)

  method    get_choices            -> returns choices + blank value
! method    get_flatchoices        -> returns flatchoices + blank value

  method    get_choices_default    -> still an alias for get_choices

comment:2 by miracle2k, 16 years ago

Has patch: set

comment:3 by Russell Keith-Magee, 16 years ago

Resolution: fixed
Status: newclosed

(In [8102]) Fixed #7913 -- Corrected backwards incompatible parts of [7977] when optgroup handling was added to field choices (Ticket #4412). Thanks to Michael Elsdorfer (miracle2k) for the report and patch.

comment:4 by evenrik, 16 years ago

Resolution: fixed
Status: closedreopened

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 evenrik, 16 years ago

Cc: evenson@… added

comment:6 by Russell Keith-Magee, 16 years ago

Resolution: fixed
Status: reopenedclosed

(In [8139]) Fixed #7913 -- Corrected ham-fisted typo from [8102]. Thanks to evinrik for the report.

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