id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 25670 `dictsort` does not work when `arg` parameter is numeric Andrew Kuchev Andrew Kuchev "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. " Bug closed Template system 1.8 Normal fixed template filter dictsort Accepted 1 0 0 0 0 0