6👍
✅
You can annotate the values of the queryset with the number of items, like:
result = genders.values('answer').annotate(
number=Count('answer')
).order_by('answer')
This will result in a QuerySet
that looks like:
<QuerySet [
{ 'answer': 'Male', 'number': 14},
{ 'answer': 'Female', 'number': 25}
]>
1👍
# if your model name is QuestionFocus, then your query should be like this
QuestionFocus.objects.values('answer').annotate(number=Count('answer'))
- [Django]-Errno – 13 Permission denied: '/media/ – Django
- [Django]-How to display a form filed with size and maxlength attributes in Django?
- [Django]-Group by in django
- [Django]-Caching results of a Django function call with cache.get_or_set()
Source:stackexchange.com