Thanks Malcom,

Looks like sorted in combination with the operator module is the key
to my problem. Now I can do:

import operator

vehicle_list = Vehicle.objects.all().select_related(depth=1)
vehicles = list(sorted(vehicle_list, key=operator.attrgetter
('model_year or whatever')))

...and do vehicles.reverse() to toggle ascending or descending.

Thanks for getting me on the right track, I appreciate it!
Brandon

On Jan 22, 2:19 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
> On Wed, 2009-01-21 at 21:40 -0800, Brandon Taylor wrote:
> > Hi everyone,
>
> > I need to do some table sorting and paging. To reduce trips to the DB,
> > I'm storing my initial queryset in a session using the file system
> > backend for local development.
>
> > I was thinking I might be able to use the dictsort filter and just
> > pass in the column as a variable to do the column sorting, but I can't
> > seem to convert a queryset into a dictionary to use with the dictsort
> > filter. Calling dict(queryset) returns an error.
>
> Simplest solution will be to write your own "sort" filter. In your
> template, that will then look like:
>
>         {{ my_queryset|sort }}
>
> A template filter is very easy to write, particularly when the filter
> takes no arguments. It's basically just a Python function that accepts
> the lhs object as a parameter. You can then use Python's sorted()
> function and pass in a comparison function or a "key" function. Have a
> read of the Python documentation for details, if you aren't familiar
> with sorted() -- it's incredibly useful.
>
> Refer to [1] for how to write template filters.
>
> [1]http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writ...
>
> Regards,
> Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to