Re: could i use a select?

2012-02-08 Thread Javier Guerra Giraldez
On Wed, Feb 8, 2012 at 9:53 AM, Dennis Lee Bieber  wrote:
> I don't
> think the slice is "present" early enough to be turned into an "offset x
> limit y" query

yes, it is; and yes, it does

-- 
Javier

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



Re: could i use a select?

2012-02-08 Thread J. Cliff Dyer
On Wed, 2012-02-08 at 09:53 -0500, Dennis Lee Bieber wrote:
> On Tue, 7 Feb 2012 21:23:58 -0800 (PST), akaariai 
> 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.comHTTP://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.



Re: could i use a select?

2012-02-07 Thread akaariai
On Feb 8, 7:08 am, Vittorino Parenti 
wrote:
> Hi,
> I've an Invoice Model:
>
> class Invoice(models.Model):
>     ...
>     number = models.IntegerField(...)
>     customer = models.ForeignKey()
>     date_invoice = ...
>     total_invoice = ...
>     ...
>
> To have the top ten customer can i do?
> I have to use a select:
>
> SELECT SUM(total_invoice) AS total FROM invoice_invoice GROUP BY
> customer ORDER BY total DESC
>
> or can I use django methods? How?

I think you could do
Customer.objects.annotate(tot_invoice=Sum(invoice_set__total_invoice)).order_by('tot_invoice')
[0:10].

 - Anssi

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