[Django] #23128: _get_FIELD_display doesn't work with Field.get_choices using Iterators

6 views
Skip to first unread message

Django

unread,
Jul 29, 2014, 12:41:45 PM7/29/14
to django-...@googlegroups.com
#23128: _get_FIELD_display doesn't work with Field.get_choices using Iterators
----------------------------------------------+-------------------------
Reporter: areski | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords: get_choices
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+-------------------------
'''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

--
Ticket URL: <https://code.djangoproject.com/ticket/23128>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 29, 2014, 12:59:29 PM7/29/14
to django-...@googlegroups.com
#23128: _get_FIELD_display doesn't work with Field.get_choices using Iterators
-------------------------------------+-------------------------------------

Reporter: areski | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: get_choices | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by charettes):

* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/23128#comment:1>

Django

unread,
Aug 5, 2014, 4:35:07 PM8/5/14
to django-...@googlegroups.com
#23128: _get_FIELD_display doesn't work with Field.get_choices using Iterators
-------------------------------------+-------------------------------------

Reporter: areski | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: get_choices | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (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>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23128#comment:2>

Django

unread,
Aug 5, 2014, 4:37:04 PM8/5/14
to django-...@googlegroups.com
#23128: _get_FIELD_display doesn't work with Field.get_choices using Iterators
-------------------------------------+-------------------------------------

Reporter: areski | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: get_choices | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by CollinAnderson):

* cc: cmawebsite@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/23128#comment:3>

Django

unread,
Nov 11, 2015, 7:46:45 AM11/11/15
to django-...@googlegroups.com
#23128: _get_FIELD_display doesn't work with Field.get_choices using Iterators
-------------------------------------+-------------------------------------
Reporter: areski | Owner: Seha16
Type: Bug | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: get_choices | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Seha16):

* owner: nobody => Seha16
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/23128#comment:4>

Django

unread,
May 7, 2016, 6:05:08 PM5/7/16
to django-...@googlegroups.com
#23128: _get_FIELD_display doesn't work with Field.get_choices using Iterators
-------------------------------------+-------------------------------------
Reporter: areski | Owner: Seha16
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | worksforme

Keywords: get_choices | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: assigned => closed
* resolution: => worksforme


Old description:

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

New description:

'''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
}}}

--

Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/23128#comment:5>

Reply all
Reply to author
Forward
0 new messages