Re: Sort by attribute not in database

2010-02-06 Thread ChrisC


On 5 Feb, 21:21, Peter Herndon  wrote:
> Never forget, Django is Python.  One way to do what you need would be to 
> implement the __cmp__ special method on your Computer class.  Then, if you 
> make e.g. a list out of the results of your QuerySet, you can call sort() on 
> the list, and you're done.
>
> The downside is that you need to make a list of your QuerySet, which may 
> require quite a bit of memory, depending on the number of objects returned.
>
> The reason you can't order_by something that's not in the database is that 
> the order_by method is SQL and is executed in the database as part of the 
> query.  The reason you can't sort() a queryset is that it's a generator, not 
> a list, so it only lazily instantiates your model objects.
>

Just the ticket. Many thanks Peter. I was doing a list comprehension
along the lines of

list =  [ x for x in Computer.objects.all() ]

which seemed to work, but your __cmp__ method looks much nicer.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Sort by attribute not in database

2010-02-05 Thread ChrisC
This may be an easy question to answer, but I just can't seem to do
this.

I have a model called Computer which has a function (turned into an
attribute via the property builtin) which queries the local filesystem
to get a modification time.

ie.
Computer.object.all()[0].last_modified

returns a time

I want to sort by this time, but unfortunately there is no sort()
method on querysets and you cannot seem to order_by() on anything
other than table fields.

Putting the field into the database isn't an option as it is a quickly
changing variable.

Any help gratefully appreciated.

Chris

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.