On Wed, 2012-02-08 at 09:53 -0500, Dennis Lee Bieber wrote: > On Tue, 7 Feb 2012 21:23:58 -0800 (PST), akaariai <akaar...@gmail.com> > wrote: > > >I think you could do > >Customer.objects.annotate(tot_invoice=Sum(invoice_set__total_invoice)).order_by('tot_invoice') > >[0:10]. > > NOTE: the OP needs a /descending/ sort order (wants the 10 highest > totals, not the 10 lowest). > > This may be an example of why I find SQL more understandable than > the object-based approaches [caveat: I'm no where near familiar with > Django's full query model], unless the back-end database feeds records > from the result set on-demand... Transferring a large result set back to > the client side just to slice off the first 10 entries just seems > inefficient over having the DBMS itself limit the data set (I don't > think the slice is "present" early enough to be turned into an "offset x > limit y" query). Having to transfer the entire result set just to access > the last 10 seems even more inefficient. > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/ >
The only change you would need to make is to change the order_by clause to .order_by('-tot_invoice'). This will not transfer the entire result set. Django implements the slicing so that it is done at the sql level. -- 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.