Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Query to get count of all distinct values for particular column [duplicate]

I am trying to fetch count of all distinct values in particular column for example I have following table in model name MyModel :

Id  City     vendor

1   Mumbai      2
2   Pune        3
3   Mumbai      1
4   Yavatmal    2
5   Pune        5

I am looking for output something like this:

[{'Mumbai':2},{'Pune':2},{'Yavatmal':1}]
like image 486
Piyush S. Wanare Avatar asked Apr 30 '16 10:04

Piyush S. Wanare


1 Answers

Like the comment of solarissmoke above. In that post you can see the solution:

from django.db.models import Count
MyModel.objects.values('city').annotate(the_count=Count('city'))
like image 186
trantu Avatar answered Sep 22 '22 13:09

trantu