[Answered ]-Django – Unexpected result from .values and .annotate

2👍

There is a subtlety here that’s not very obvious at first glance because the ProductNoSerialInstance model has a field named name which is actually a foreign key. Couple that with the fact that you are getting values and this is the result. values return dictionaries rather than object instances so {{ item.name.name }} isn’t unfortunately going to work. you need to make a slight modification to your query

ProductNoSerialInstance.objects.values('name__name').annotate(count=models.Count('name'))
👤e4c5

Leave a comment