Greetings,

I have two tables: Student and Grade in a one to many relationship.
The Grade table has a field named "grade" which has integers from 0 to
5.
I want to count how many grades of each type each Student has. For
this I'm using a query similar to:
Students.objects.order_by('grades__grade').annotate(n=Count('grades'))
This SQL query does a group by Student.id and Grade.grade and orders
the results by Grade.grade. The result set is something like:
student1  2
student2  4
student3  6
student2  5
student1  3
student2  4
student3  7
As you can see there is no way of telling what grade the count was
for. What I would like is something more like:
student1  2  2
student2  4  2
student3  6  3
student2  5  3
student1  3  4
student2  4  4
student3  7  4
This means I would like somehow to add to the results the column used
in the group by statement.
A hack-ish solution is to use this query instead:
Students.objects.order_by('grades__grade').annotate(n=Count
('grades')).annotate(g=Avg('grades'))
but it doesn't feel right. I've also tried to do a .values
('grades_grade') but values() doesn't seem to work across relations.

Is there a better way to do this using the ORM?
Thanks

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to