Retrieving other fields along with ValueQueryset

26 views
Skip to first unread message

Joel Goldstick

unread,
Nov 2, 2014, 2:19:08 PM11/2/14
to django...@googlegroups.com
I have a model with a year field and a score field, as well as other
fields. I want to get the highest score for each year. I can do that
with this:

Batting.objects.values('year').annotate(category=Max('score'))

But I also want to display the the name associated with the highest
score. The name is in a table that is linked by a foreign key. My
results contain only the year and the Max value.

How can I get the name in my queryset but still group by year only?


--
Joel Goldstick
http://joelgoldstick.com

Joel Goldstick

unread,
Nov 2, 2014, 3:08:19 PM11/2/14
to django...@googlegroups.com
Using v1.6

Collin Anderson

unread,
Nov 12, 2014, 5:08:11 PM11/12/14
to django...@googlegroups.com
Hello,

I feel like this sort of query comes up a lot and there isn't a good solution.

One option would be to somehow do a _second_ query that gets all of the Batting objects for each of the year, score pairs.

Or, you could a query for each year and sort by score.

Or, it might be possible to get the info using extra().

You could also do the sorting and grouping by hand using python if there's not too much data.

Collin
Reply all
Reply to author
Forward
0 new messages