Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added more tests and documentation for dictsort.

It's possible to use something like {{ foo|dictsort:'bar.baz' }}
but this wasn't tested or documented.
  • Loading branch information...
commit b914991b3705cb6c91013d962c55cda9deb18d83 1 parent c349bcb
@bmispelon bmispelon authored
Showing with 35 additions and 0 deletions.
  1. +21 −0 docs/ref/templates/builtins.txt
  2. +14 −0 tests/defaultfilters/tests.py
View
21 docs/ref/templates/builtins.txt
@@ -1433,6 +1433,27 @@ then the output would be:
{'name': 'zed', 'age': 19},
]
+You can also do more complicated things like::
+ {{ for book in books|dictsort:"author.age"
@charettes Collaborator

I guess you meant {% for book in books|dictsort:"author.age" %}.

@bmispelon Collaborator

Yes, thanks for catching (and fixing) this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * {{ book.title }} ({{ book.author.name }})
+ {% endfor %}
+
+If ``books`` is:
+
+.. code-block:: python
+
+ [
+ {'title': '1984', 'author': {'name': 'George', 'age': 45}},
+ {'title': 'Timequake', 'author': {'name': 'Kurt', 'age': 75}},
+ {'title': 'Alice', 'author': {'name': 'Lewis', 'age': 33}},
+ ]
+
+the the output would be::
@claudep Collaborator
claudep added a note

s/the/then

@bmispelon Collaborator

Fixed it, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ * Alice (Lewis)
+ * 1984 (George)
+ * Timequake (Kurt)
+
.. templatefilter:: dictsortreversed
dictsortreversed
View
14 tests/defaultfilters/tests.py
@@ -441,6 +441,20 @@ def test_dictsort(self):
self.assertEqual(dictsort({'a': 1}, 'age'), '')
self.assertEqual(dictsort(1, 'age'), '')
+ def test_dictsort_complex_sorting_key(self):
+ """
+ Since dictsort uses template.Variable under the hood, it can sort
+ on keys like 'foo.bar'.
+ """
+ data = [
+ {'foo': {'bar': 1, 'baz': 'c'}},
+ {'foo': {'bar': 2, 'baz': 'b'}},
+ {'foo': {'bar': 3, 'baz': 'a'}},
+ ]
+ sorted_data = dictsort(data, 'foo.baz')
+
+ self.assertEqual([d['foo']['bar'] for d in sorted_data], [3, 2, 1])
+
def test_dictsortreversed(self):
sorted_dicts = dictsortreversed([{'age': 23, 'name': 'Barbara-Ann'},
{'age': 63, 'name': 'Ra Ra Rasputin'},
@charettes

I guess you meant {% for book in books|dictsort:"author.age" %}.

@bmispelon

Yes, thanks for catching (and fixing) this.

@claudep

s/the/then

@bmispelon

Fixed it, thanks.

Please sign in to comment.
Something went wrong with that request. Please try again.