Code

Opened 6 years ago

Closed 6 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: master
Severity: Keywords:
Cc: evenson@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 6 years ago.

Download all attachments as: .zip

Change History (7)

Changed 6 years ago by miracle2k

comment:1 Changed 6 years ago by miracle2k

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 6 years ago by miracle2k

  • Has patch set

comment:3 Changed 6 years ago by russellm

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

(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 Changed 6 years ago by evenrik

  • Resolution fixed deleted
  • Status changed from closed to 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 Changed 6 years ago by evenrik

  • Cc evenson@… added

comment:6 Changed 6 years ago by russellm

  • Resolution set to fixed
  • Status changed from reopened to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.