[Django]-Django Queryset Count

9👍

from django.db.models import Count

Device.objects.values('build').annotate(count=Count('pk'))
# -> [{'build': '1', 'count': 3}, {'build': '2', 'count': 4}]

4👍

A simple approach is just calling count:

>>> Device.objects.filter(build='1').count()
3
>>> Device.objects.filter(build='2').count()
4

Leave a comment