0

I've seen a SO post on this but can't seem to find it.

My results are rendering in my template with the object_name as
{'sessions__sum': 29649}

instead of just
29649.

How can I show just the sum? Thank you.

Update

I realize, out of lack of knowledge, I may not have asked my question correctly, so below is my view and a screenshot of my template, rendered in the browser. Hopefully this will help clear things up.

view

year_weekly_filter.filter(created__week_day=1).aggregate(Sum('sessions'))
monday2014 = year_weekly_filter.filter(created__week_day=2).aggregate(Sum('sessions'))

Template

{{ monday2014 }}

ScreenShot - how its rendering in the browser

enter image description here

Charles Smith
  • 3,201
  • 4
  • 36
  • 80
  • If you're going to down vote, then you should provide some professional and useful feedback. If not for me, at least for other users that may see this post. – Charles Smith Mar 23 '16 at 08:25

2 Answers2

1

Do you mean the {{object}} in the template is rendered by the dictionary object? In this case, the key is accessed in this way.

{{object.sessions__sum}}

and you may get help in this and the related documentation is here.

I don't have enough points to add a comment, so I leave my guess here.

Community
  • 1
  • 1
Leonard2
  • 894
  • 8
  • 21
1

You should note that: "aggregate() is a terminal clause for a QuerySet that, when invoked, returns a dictionary of name-value pairs." - Django Docs
If you just want a single value to be returned to a variable and not a dict, use .get like so:

monday2014 = year_weekly_filter.filter(created__week_day=2).aggregate(Sum('sessions')).get('sessions__sum',0)
Georgina S
  • 467
  • 2
  • 9
  • I have been banging my head for a day trying to figure this out...just didn't know how to ask the question. Thank you!!! – Charles Smith Mar 24 '16 at 14:53
  • No problem! I came across this recently in my own work and was baffled by it for a bit, makes sense to return a dict for creating multiple aggregations in a single query. – Georgina S Mar 24 '16 at 15:09