{{{
@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.
* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted
Comment:
This ticket is a spinoff of #25646.
--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:1>
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:2>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/5551 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:3>
Comment (by timgraham):
I'm not sure about this. It seems like the old behavior was somewhat
accidental. Having a filter called "dictsort" work on a list of lists
seems a bit odd and unintuitive. Maybe we should ask for other opinions on
the django-developers mailing list. Maybe there is a common third-party
filter that would do the job that we could add to builtins.
--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:4>
Comment (by timgraham):
After some further thought, I changed my mind given the fact that dictsort
doesn't work with numeric string keys. That's definitely a bug that should
be fixed.
--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:5>
* needs_better_patch: 0 => 1
Comment:
I left comments for improvement on the pull request.
--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:6>
* needs_better_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:7>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"e81d1c995c0cc5573d3627de0fe6b803b2f43fb2" e81d1c99]:
{{{
#!CommitTicketReference repository=""
revision="e81d1c995c0cc5573d3627de0fe6b803b2f43fb2"
Fixed #25670 -- Allowed dictsort to sort a list of lists.
Thanks Tim Graham for the review.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/25670#comment:8>