[Answered ]-Annotate by extra field

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 the annotate(), the annotation will be computed using the grouping described by the values() clause.

Leave a comment