Tim,

great answer, but I'm thinking I might need to build a true queryset
myself.

I was really hoping to hack the queryset model to allow this to happen
in the django classes.

Unless I'm missing something, the examples given, can I use those
'basically' as querysets in my templates?  i.e. display, fields, etc?
I think so

it would be really cool if I could intercept the queryset.orderby()
routine to determine if its a calced field and sort internally, but
that would probably throw the whole django ORM into chaos, no?

I'll try some of these examples for now and see how they work, thanks
for everyone's response!

John


On May 24, 7:15 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> > If I have a model that returns calculated fields, how would I sort a
> > query set on them?
>
> > Assume the query sets are relatively small (< 100) for now.
>
> Give this condition (a small set of data) and no need to slice
> the results on the server based on this order, it can be done
> purely in python.  If the data-set grows larger, it will be more
> efficient to do some hacks to get this on the server side of
> things.  However...assuming those conditions:
>
> For the below example code, "foo_list" is a list of Foo objects
> with an "x" and a "y" property.  For this example,
> some_function() determines the magnitude-squared (x**2 + y**2)
>
> >>> foo_list = list(Foo.objects.all())
> >>> foo_list
>
> [<Foo x=1, y=1>, <Foo x=4, y=9>, <Foo x=9, y=7>, <Foo x=10, y=2>,
> <Foo x=2, y=9>, <Foo x=4, y=10>, <Foo x=5, y=7>, <Foo x=2, y=3>,
> <Foo x=9, y=2>, <Foo x=2, y=3>]>>> # sort them based on "x"
> >>> foo_list.sort(cmp=lambda a,b: cmp(a.x, b.x))
> >>> foo_list
>
> [<Foo x=1, y=1>, <Foo x=2, y=9>, <Foo x=2, y=3>, <Foo x=2, y=3>,
> <Foo x=4, y=9>, <Foo x=4, y=10>, <Foo x=5, y=7>, <Foo x=9, y=7>,
> <Foo x=9, y=2>, <Foo x=10, y=2>]>>> # sort them based on "y"
> >>> foo_list.sort(cmp=lambda a,b: cmp(a.y, b.y))
> >>> foo_list
>
> [<Foo x=1, y=1>, <Foo x=9, y=2>, <Foo x=10, y=2>, <Foo x=2, y=3>,
> <Foo x=2, y=3>, <Foo x=5, y=7>, <Foo x=9, y=7>, <Foo x=2, y=9>,
> <Foo x=4, y=9>, <Foo x=4, y=10>]>>> # sort it by the results of the 
> some_function() call
> >>> foo_list.sort(cmp=lambda a,b: cmp(a.some_function(),
> b.some_function()))
> >>> foo_list
>
> [<Foo x=1, y=1>, <Foo x=2, y=3>, <Foo x=2, y=3>, <Foo x=5, y=7>,
> <Foo x=9, y=2>, <Foo x=2, y=9>, <Foo x=4, y=9>, <Foo x=10, y=2>,
> <Foo x=4, y=10>, <Foo x=9, y=7>]
>
> Note that the Python sort() function
> 1) does the sort in-place and
> 2) does *not* return the results (returns None instead)
>
> Hope this helps,
>
> -tim


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to