You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now 'autosort' tag assumes that what is passed as an var is a queryset. I guess this assumption could be relaxed a bit to any iterables. The solution below (diff) works well for me.
diff -crB django_sorting-old/templatetags/sorting_tags.py django_sorting/templatetags/sorting_tags.py
*** django_sorting-old/templatetags/sorting_tags.py 2010-02-20 18:56:52.000000000 +0100
--- django_sorting/templatetags/sorting_tags.py 2010-02-20 19:03:45.000000000 +0100
***************
*** 1,6 ****
--- 1,8 ----
from django import template
from django.http import Http404
from django.conf import settings
+ from django.db.models.query import QuerySet
+ from operator import attrgetter
register = template.Library()
***************
*** 96,102 ****
order_by = context['request'].field
if len(order_by) > 1:
try:
! context[key] = value.order_by(order_by)
except template.TemplateSyntaxError:
if INVALID_FIELD_RAISES_404:
raise Http404('Invalid field sorting. If DEBUG were set to ' +
--- 98,119 ----
order_by = context['request'].field
if len(order_by) > 1:
try:
! if isinstance(value, QuerySet):
! # more flexible but generally more error-prone check:
! # callable(getattr(value, 'order_by', None))
! context[key] = value.order_by(order_by)
! # sort iterable
! elif hasattr(value, '__iter__'):
! if order_by[0]=='-': # descending order
! reverse = True
! order_by = order_by[1:]
! else: # ascending order (standard)
! reverse = False
! context[key] =\
! sorted(value,key=attrgetter(order_by),reverse=reverse)
! else:
! raise AttributeError("Expected QuerySet or iterable under\
! template variable '%s'." % key)
except template.TemplateSyntaxError:
if INVALID_FIELD_RAISES_404:
raise Http404('Invalid field sorting. If DEBUG were set to ' +
The text was updated successfully, but these errors were encountered:
This was very useful for sorting dictionaries, and worked with one small modification:
sorted(value,key=itemgetter(order_by),reverse=reverse)
It does not seem to allow sorting on related object fields when the object is the value in a dictionary.
Right now 'autosort' tag assumes that what is passed as an var is a queryset. I guess this assumption could be relaxed a bit to any iterables. The solution below (diff) works well for me.
The text was updated successfully, but these errors were encountered: