I'm very new to Django , I'm trying to return the sum of total revenue attained by selling cars with respect to a particular site in Django . I have tried the annotate method to fetch the results. I have written a query set using annotate.
mum_data = Cars_Showroom_Filter(request.GET, queryset = Cars.objects.filter(Site__in = ( "Mumbai",)).annotate(price=Sum('price')))
This returns me a filter object <users.filters.Cars_Showroom_Filter object at 0x00000264ABBA2A90> and I'm unable to read it . I tried using qs.count() but it returns me the entire query set list . Can some one help me with a way to read this particular query set value
CodePudding user response:
Just do something like this,I hope it helps !
queryset = Cars.objects.filter(Site__in = ( "Mumbai",)).annotate(price=Sum('price'))
CodePudding user response:
This returns me a filter object <users.filters.Cars_Showroom_Filter object at 0x00000264ABBA2A90>
annotate() return a queryset you can get sum of each indivisual like this
by iterating over queryset
for item in queryset:
item.field__sum
this will return you sum of perticular field check here Sum()
what you want is aggregate() this will return total of column like this
mum_data = Cars_Showroom_Filter(request.GET, queryset = Cars.objects.filter(Site__in = ( "Mumbai",)).aggregate(price=Sum('price')))
this will return you this value
{'price__sum': 127183} # this is random value it will return your price total