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

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

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

php - Cloud9 cloud IDE and CakePHP -