Reputation: 477
My model:
class Device(models.Model):
build = models.CharField()
name = models.CharField()
How do I build my queryset so I can get the count of objects with different builds.
For example if there were two builds 'build 1' and 'build 2', I would want an output that would tell me
build 1 = 3
build 2 = 4
EDIT: Tried the following:
Device.objects.values('build').annotate(count=Count('pk'))
the output is:
[{'build': u'wed build'}, {'build': u'red build'}, ... ]
Upvotes: 3
Views: 7648
Reputation: 4269
A simple approach is just calling count
:
>>> Device.objects.filter(build='1').count()
3
>>> Device.objects.filter(build='2').count()
4
Upvotes: 4
Reputation: 62908
from django.db.models import Count
Device.objects.values('build').annotate(count=Count('pk'))
# -> [{'build': '1', 'count': 3}, {'build': '2', 'count': 4}]
Upvotes: 9