Created
April 25, 2016 21:30
-
-
Save samukasmk/d5cc56c33c37033f78fb94a4cb5e85c6 to your computer and use it in GitHub Desktop.
Django example of SELECT Distinct Count by values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Model example | |
class Person(models.model): | |
... | |
country = moldels.CharField(...) | |
# Select counting by values of field country | |
>>> Person.objects.all().values("country").annotate(total=Count("country")) | |
>>> [ | |
{ | |
"country": "Brasil", | |
"total": 7 | |
}, | |
{ | |
"country": "Denmark", | |
"total": 5 | |
}, | |
{ | |
"country": "Egypt", | |
"total": 1 | |
}, | |
{ | |
"country": "Finland", | |
"total": 1 | |
}, | |
{ | |
"country": "France", | |
"total": 1 | |
}, | |
{ | |
"country": "Germany", | |
"total": 1 | |
}, | |
{ | |
"country": "Israel", | |
"total": 1 | |
}, | |
{ | |
"country": "United States of America", | |
"total": 1 | |
}, | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! It helped me a lot!