#25670: `dictsort` does not work when `arg` parameter is numeric
---------------------------------+--------------------------------------
Reporter: soon | Owner: soon
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Normal | Keywords: template filter dictsort
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
According to `dictsort` documentation, it orders given list of
dictionaries, using `arg` as property in each dictionary:
{{{
@register.filter(is_safe=False)
def dictsort(value, arg):
"""
Takes a list of dicts, returns that list sorted by the property given
in
the argument.
"""
try:
return sorted(value, key=Variable(arg).resolve)
except (TypeError, VariableDoesNotExist):
return ''
}}}
However, it is not possible to order list of dictionaries by a numeric
key. Let's consider the following test case:
{{{
def test_sort_list_of_tuple_like_dicts(self):
data = [{'0': 'a', '1': '42'},
{'0': 'c', '1': 'string'},
{'0': 'b', '1': 'foo'}]
sorted_data = dictsort(data, '0')
self.assertEqual([{'0': 'a', '1': '42'},
{'0': 'b', '1': 'foo'},
{'0': 'c', '1': 'string'}], sorted_data)
}}}
This test fails with the following message:
{{{
Traceback (most recent call last):
File ".../django/tests/template_tests/filter_tests/test_dictsort.py",
line 50, in test_sort_list_of_tuple_like_dicts
{'0': 'c', '1': 'string'}], sorted_data)
AssertionError: Lists differ: [{'0': 'a', '1': '42'}, {'0': 'b', '1':
'foo'}, {'0': 'c', '1': 'string'}] != [{'0': 'a', '1': '42'}, {'0': 'c',
'1': 'string'}, {'0': 'b', '1': 'foo'}]
First differing element 1:
{'0': 'b', '1': 'foo'}
{'0': 'c', '1': 'string'}
- [{'0': 'a', '1': '42'}, {'0': 'b', '1': 'foo'}, {'0': 'c', '1':
'string'}]
+ [{'0': 'a', '1': '42'}, {'0': 'c', '1': 'string'}, {'0': 'b', '1':
'foo'}]
}}}
The `dictsort` uses `sorted` function with `key=Variable(arg).resolve`.
When `arg` is `'0'`, `key` function should behave like
`operator.itemgetter('0')`, but `Variable('0').resolve(context)` returns
`0` regardless of given `context`.
There are five usages of `dictsort` with `"0"` as `arg` in
[https://github.com/django/django/blob/master/django/views/debug.py#L816
debug.py]
As [https://code.djangoproject.com/ticket/25646#comment:4 mentioned] by
''bmispelon'', this may be some kind of regression in Django 1.3.
--
Ticket URL: <https://code.djangoproject.com/ticket/25670>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/047.dfa1b19698c760f4369286464bab137d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.