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 Hope that helps, ian On 5/25/06, Jay Parlar <[EMAIL PROTECTED]> wrote: > > I'm seeing some strange slicing behaviour (using sqlite3 and the dev server). > > All I want to do is grab the last element in the table, so I'm trying this: > > Foo.objects.all()[-1] > > When I do that in a shell (via manage.py shell) it works fine, and > respects my 'ordering' Meta. However, when my application is actually > running, that same line will ALWAYS return the first row in the table, > not the last one. > > Am I missing something here? > > 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 -~----------~----~----~----~------~----~------~--~---