Reputation: 8774
I am having a hard time putting this question into words, so I apologize. Here's the situation.
I have a table of user's Foursquare Checkins. Each checkin has a foreign key to a location. I'd like to say "Show me all user checkins, sorted by how many times they checked into each location, with the checkin count as part of the query count" I also dont want the location repeated in the list.
So imagine this is the user checkin list:
A
A
B
A
A
B
B
C
I'd want the return to be
A (4)
B (3)
C (1)
Is this something I should do with the query, or just query them all and do this in Python afterwards?
Brenden
Upvotes: 1
Views: 548
Reputation: 87215
You just annotate the Checkin model and order it in reverse
from django.db.models import Count
Checkins.objects.filter(user=my_user).annotate(chkn_count=Count('location')).order_by('-chkn_count')
Upvotes: 1