Opened 2 years ago

Closed 7 weeks ago

#23128 closed Bug (worksforme)

_get_FIELD_display doesn't work with Field.get_choices using Iterators

Reported by: areski Owned by: Seha16
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: get_choices
Cc: cmawebsite@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by timgraham)

Context:
This issue has been noted when fixing https://code.djangoproject.com/ticket/23112

Current behavior:
Using _get_FIELD_display on a Field.choices using Iterators, you will get the first tuple value.

Expected behavior:
Expected result is to get the human-readable name in from the iterator tuple.

Tests to reproduce the issue:

Edit tests/model_fields/tests.py add inside class ChoicesTests(test.TestCase) the following tests:

def test_iterator_choices_and_field_display(self):
    """
    Check that get_choices works with Iterators (#23112).
    """
    self.assertEqual(WhizIter(c=1).get_c_display(), 'val-1') # A nested value
    self.assertEqual(WhizIter(c=9).get_c_display(), 9) # Invalid value
    self.assertEqual(WhizIter(c=None).get_c_display(), None) # Blank value
    self.assertEqual(WhizIter(c='').get_c_display(), '') # Empty value

Change History (5)

comment:1 Changed 2 years ago by charettes

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 23 months ago by CollinAnderson

that's odd. it works for me.

In [2]: Address(country='CA').get_country_display()
Out[2]: u'Canada'

In [3]: Address(country='US').get_country_display()
Out[3]: u'United States'

In [4]: import django; django.get_version()
Out[4]: '1.8.dev20140805124457'

In [5]: Address._meta.get_field('country').choices
Out[5]: <itertools.tee at 0x2d978c0>

comment:3 Changed 23 months ago by CollinAnderson

  • Cc cmawebsite@… added

comment:4 Changed 8 months ago by Seha16

  • Owner changed from nobody to Seha16
  • Status changed from new to assigned

comment:5 Changed 7 weeks ago by timgraham

  • Description modified (diff)
  • Resolution set to worksforme
  • Status changed from assigned to closed

I don't think there's an issue here. The Counter used for the choices in WhizIter generates values starting from two, not one so the provided test isn't correct. If you change the first line to self.assertEqual(WhizIter(c=2).get_c_display(), 'val-2') it passes.

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