annotate sum many record

23 views
Skip to first unread message

carlos

unread,
Jun 3, 2019, 2:48:27 AM6/3/19
to django...@googlegroups.com
Hi, i used django 1.11 and try used annotate

i have model with 3 decimal fields i would like sum 2 and multiply for other field but with aritmetic calculation
i have used this query

(1) total = Model.objects.annotate(all=Sum(F('fieldA') + F('fieldB') * F('fieldC')))
but this return queryset with all calculation i have 3 record in db
print total  
and return this

<QuerySet [<Model: Model object>, <Model: Model object>, <Model: Model object>]>

then I do a forloop with for to do the sum
for x in total:
    variable += x.all 

it is possible to return the sum of the 3 registers with the annotate in a single line, 
as (1)total queryset

How could I do that?

thank for yours helps


--
att.
Carlos Rocha

Aditya Singh

unread,
Jun 3, 2019, 2:50:53 AM6/3/19
to django...@googlegroups.com
Hello mate,
Simple get the value from the objects and store in separate variables. Then you may do any and as many calculations you want and return the desired value :)
Kind Regards,
Aditya

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAM-7rO1LGRSMoBMC435nf93P-__oo%3DhYz_hwBiW6WxVeip8uvw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages