I have a table that holds multiple tracking id’s in it. What I’m trying to do is group all tracking id’s and get a count for them.
This is what the SQL query would look like:
SELECT tracking_id, COUNT( * )
FROM tracking
GROUP BY tracking_id
I think I’ve found the correct method of doing this in django however I’m missing something and am currently getting the error Exception Value: 'dict' object has no attribute 'tracking_id'
Here’s what my view looks like:
def stats(request):
users = Tracking.objects.values('tracking_id').annotate(dcount=Count('tracking_id'))
stats = []
for user in users:
stats.append((user.tracking_id, user.dcount))
return render_to_response('tracking/stats.html', { 'stats': stats, })
Any help would be appreciated!
How about (partially inspired by @second’s answer):
and then
?