Math in Django to get a total returned -


i'm trying result tells me 10.5 hours worked on project.

here's i'm doing in view:

time = time.objects.filter(my_company=my_company, project=project) raw_hours = time.aggregate(sum('hours')) raw_minutes = time.aggregate(sum('minutes')) adj_minutes = raw_minutes.raw_minutes__sum / 100 hours = adj_minutes + raw_hours.raw_hours__sum return {'hours': hours} 

when add {{ view.hours }} template, i'm not seeing in return. i'm expecting view.hours == 10.5 or depending on math.

so have 2 questions. thinking correctly , there more efficient way this?

thanks help.

you can both aggregates doing:

d = time.aggregate(sum('hours'), sum('minutes')) 

this should return dictionary looks this:

{'hours__sum' : decimal('10'), 'minutes__sum' : decimal('630')} 

so access values should deal same way deal other dictionary.

hours = d['hours__sum'] 

on template, need provide key context dictionary, in case:

{{ hours }} 

Comments

Popular posts from this blog

angularjs - Showing an empty as first option in select tag -

qt - Change color of QGraphicsView rubber band -

c++ - Visible files in the "Projects" View of the Qt Creator IDE if using the CMake build system -