Tuesday, February 7, 2012

Re: could i use a select?

On Feb 8, 7:08 am, Vittorino Parenti <vpare...@thundersystems.it>
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment