On 5/7/2009 10:42 AM, overgroove wrote:
> I have the following models:
> 
> Customer
> Order
> Lineitem
> 
> Customers have orders and orders have lineitems.  In lineitems there's
> a product price and a product quantity.  I would like to be able to,
> using django querysets and whatever magic I have to do, to annotate
> the multiplication of price and quantity in lineitem to the customer.
> I know I can do this with raw sql, however, I'd like to do it with
> querysets so that I can afterwards apply a filter.  I've tried a hack
> where I perform some trivial annotation to the customer based on a
> lineitem (which gives me the appropriate joins) and then add SUM(price
> * quantity) with extra() to the select.  However, this column also
> gets added to the group by clause, rendering the query useless.

There might be a fancy way to do this in QuerySet, but I don't know how.

I personally would just write a custom method on Customer or Order to do 
this for you, depending on what level of aggregation you need.

-- 
George

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

Reply via email to