David Pekker
David Pekker

Reputation: 353

most django/pythonic way to find the most popular choice in queryset

there is a model field with choices:

class MyModel(models.Model):
     your_choice = models.CharField(choices=('A', 'B', 'C', 'D'))

what is the most compact way to get the most popular choice from the queryset?

If for example, the queryset is:

qs = MyModel.objects.all()

Upvotes: 0

Views: 65

Answers (1)

arjunattam
arjunattam

Reputation: 2819

You can try

qs.values('your_choice').annotate(dcount=Count('your_choice'))

Upvotes: 1

Related Questions