- Author:
- gmandx
- Posted:
- July 2, 2009
- Language:
- HTML/template
- Version:
- Not specified
- Score:
- 1 (after 1 ratings)
Variation on dictsort using attribute access. Nested attributes can be used, like, "obj.attr.attr_attr"
Example usage:
{% for entry in entries|sortby:'category.title' %}
Based on 1609
1 2 3 4 5 6 7 8 9 10 11 12 13 | def sortby(sequence, attribute):
"""
Variation on dictsort using attribute access
Nested attributes can be used, like, "obj.attr.attr_attr"
"""
def deep_attr(obj, attr_list):
if len(attr_list) == 1:
return getattr(obj, attr_list[0])
return deep_attr(getattr(obj, attr_list[0]), attr_list[1:])
lst = list(sequence)
lst.sort(key=lambda obj: deep_attr(obj, attribute.split('.')))
return lst
|
More like this
- Bootstrap Accordian by Netplay4 5 years, 10 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
- Bootstrap theme for django-endless-pagination? by se210 8 years, 10 months ago
- Reusable form template with generic view by roldandvg 8 years, 11 months ago
- Pagination Django with Boostrap by guilegarcia 9 years, 1 month ago
Comments
Very nice.
#
Please login first before commenting.