Sunday, June 2, 2019

Re: annotate sum many record

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

On Mon, Jun 3, 2019, 12:18 PM carlos <crocha09.09@gmail.com> wrote:
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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAEPfumi6LzOigpLiSmWuuyC7Wg7_U5_haSLYCksJcO0wbr5D6g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment