On Fri, Mar 4, 2011 at 8:40 PM, Sidney Cadot <sid...@jigsaw.nl> wrote: > Hi Tom, > >> Unless I'm being dense, you cannot represent this as a single SQL >> query, so logically you cannot represent this as a QuerySet. > > It is possible by using a nested query, e.g. > > SELECT * FROM (SELECT * FROM some_table ORDER BY timestamp DESC LIMIT 10) > ORDER BY timestamp ASC;
MySQL at least will barf on an limited subquery: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' I'm not sure what versions do, if any. > > But I suppose QuerySets are designed to map onto a single, non-nested query > (I don't know the innards of Django I'm afraid) so such a construct would be > out of reach. > An alternative would be to augment querysets such that it would be possible > to iterate over them in reverse order. > QuerySets aren't designed that way at all, most filter arguments that take a list will also take a queryset, and the new composed queryset will have a sub-query. Eg: UsageLogEntry.objects.filter( id__in=UsageLogEntry.objects.all().order_by('-time')[:10] ).order_by('time') would be perfectly valid as far as Django is concerned, but unstable and non-portable from a DB perspective (MySQL won't allow that). Reverse iterators over a queryset would be great, but would require either caching on the queryset result (in which case, you're back to "It's trivial in python") or reverse iterator support for the database API, which again, don't exist. Cheers Tom -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.