I’m working on a QA system in django, which includes data tables of Question, Answer and Answer_statistics. One Question can have multiple Answers, an Answer has an Answer_statistics. Answer_statistics contain values like votes count, comments count of each answer. Now I’m trying to get the sum of a column in answer_statistics filtered by the question the answers are attached to. For example, get the total vote count of all the answers to a certain question. It should be something like this:
a_question.answer__answer_statistics_set.aggregate(Sum('comment_count'))
Feels like there should be some kind of easy solutions, but couldn’t find one by now. Could someone please give a hint? Thanks!
You follow the relationship inside the aggregate call, not in the object lookup. Something like:
a_question.aggregate(Sum('answer__answer_statistics__comment_count'))