Sunday, June 2, 2019

annotate sum many record

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.

No comments:

Post a Comment