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

1
  • 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. Commented Mar 23, 2016 at 8:25

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

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!!!
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.