On 5/25/06, Ian Maurer <[EMAIL PROTECTED]> wrote:
>
> Django's QuerySet handles slicing through the LIMIT and OFFSET clauses
> of the database. Since the clauses cannot handle python's "negative
> indexing" scheme, you have 2 choices:
>
> 1. Do the actual evaluation, by converting to a list and then doing your 
> slice:
>
>    list(Foo.objects.all())[-1]
>
> 2. Use an order_by method to set the reverse order and then get the
> first item, use a minus sign to denote DESC order:
>
>    Foo.objects.order_by("-column")[0]
>
> The second option is FAR superior. The first solution is, IMO, totally
> unacceptable since it retrieves all of the records just to access one
> row.
>
> For more information about the amazing Django QuerySets, check out this link:
>
> http://www.djangoproject.com/documentation/db_api/
>
> This section discusses slicing specifically:
>
> http://www.djangoproject.com/documentation/db_api/#limiting-querysets
>

Perfect, the 'order_by' worked beautifully!

Maybe a note should go into the db_api docs about -1 not working? I
did look at those docs before sending my original message, and didn't
see anything about that.

Jay P.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to