On Tue, 2006-07-18 at 05:04 +0000, sasha wrote:
> Hi,
>
> I must be missing something really obvious - QuerySets don't support
> negative notation, so what is then the right way of getting at the last
> item in the set?
Two possibilities:
(1) If you only want the last items and not the first items, then
reverse the ordering condition on your query set (exactly how to do this
depends upon how you're constructing the original query. You want to do
things like order by '-creation_time' instead of 'creation_time' and so
forth).
(2) If you need items at both ends of the query set, turn it into a list
(which will read all the results into memory) and then you can access
elements at random:
item = list(queryset)[-1]
Finally, for really huge result sets (tens of thousands of lines), if
reading it into memory might be a bad idea, you can write two queries:
one to get the early results and one ordered in the opposite direction
to get the later results. But this case will be rare. Normally one of
option (1) or (2) will suffice.
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---