Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalization of 'autosort' tag for iterables. #8

Open
trybik opened this issue Feb 20, 2010 · 1 comment
Open

Generalization of 'autosort' tag for iterables. #8

trybik opened this issue Feb 20, 2010 · 1 comment

Comments

@trybik
Copy link

trybik commented Feb 20, 2010

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 ' +
@alsaihn
Copy link

alsaihn commented Mar 1, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants