Django

Code

Ticket #7913 (closed: fixed)

Opened 4 months ago

Last modified 4 months ago

Backwards incompatible change by #4412 / r7977: get_FIELD_display() returns blank value

Reported by: miracle2k Assigned to: nobody
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: evenson@gmail.com Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

7913.diff (2.5 kB) - added by miracle2k on 07/24/08 10:51:37.

Change History

07/24/08 10:51:37 changed by miracle2k

  • attachment 7913.diff added.

07/24/08 11:00:32 changed by miracle2k

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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

07/24/08 11:01:42 changed by miracle2k

  • has_patch set to 1.

07/27/08 02:22:39 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

(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.

07/29/08 12:53:55 changed by evenrik

  • status changed from closed to reopened.
  • resolution deleted.

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)

07/29/08 12:56:22 changed by evenrik

  • cc set to evenson@gmail.com.

07/29/08 16:43:08 changed by russellm

  • status changed from reopened to closed.
  • resolution set to fixed.

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


Add/Change #7913 (Backwards incompatible change by #4412 / r7977: get_FIELD_display() returns blank value)




Change Properties
Action