2👍
places = places.values('first_letter').annotate(count=Count('id')).order_by()
‘order_by()’ is required for your count, see here https://docs.djangoproject.com/en/1.7/topics/db/aggregation/#order-by
0👍
You should annotate with a count of different places (having the same first letter):
p = Place.objects.all()
p = p.extra(select={'first_letter': 'SUBSTR(name,1,1)'})
p = p.values('first_letter').annotate(count=Count('id'))
You have to choose the field to annotate which is unique for every place. So ID is the best fit.
Interesting the behavior of the values
method used with annotations:
If the
values()
clause precedes theannotate()
, the annotation will be computed using the grouping described by thevalues()
clause.
- [Answered ]-Django query database and send data to template
- [Answered ]-Forbidden 403 during CSRF check with an AJAX POST request in Django
Source:stackexchange.com