python - Data Aggregation in Django Model -
this model concerned:
class order(models.model): guest = models.foreignkey('hotel.guest', on_delete=models.cascade) date = models.datefield(auto_now_add=true) amount = models.integerfield() item = models.foreignkey('hotel.item', on_delete=models.cascade) price = models.integerfield(default=1) is_paid = models.booleanfield(default=false) paid_date = models.datefield(blank=true, null=true)
my current view returns list specific guest of unpaid orders:
open_orders = order.objects.filter(guest = guest, is_paid=false).
i aggregate amount , price per item , date list in new list...
the documentation explains how aggregations across entire queryset, , annotations across relationship seem stuck this...
i used .values('item_id','date').annotate(sum('amount')).annotate(sum('price'))
doesn't seem right.
data example:
open_orders = (date=11/11/2011,guest=john,amount=2,item=beer,price = 30 = order.amount * order.item.price, is_paid = false, paid_date null), (11/11/2011,john,4,beer,60,false,null), (11/11/2011,john,2,wine,50,false,null), (11/12/2011,john,2,beer,30,false,null), (11/12/2011,john,1,wine,25,false,null), (11/12/2011,john,8,wine,200,false,null)
the desired output be
(11/11/2011,john,6,beer,90,false,null), (11/11/2011,john,2,wine,50,false,null), (11/12/2011,john,2,beer,30,false,null), (11/12/2011,john,9,wine,225,false,null)
Comments
Post a Comment