207👍
✅
Count
can take a distinct
argument, like so:
p = Project.objects.all().annotate(Count('informationunit__username',
distinct=True))
This doesn’t seem to be documented, but you can find it in the source for Count.
53👍
If you just want to count the distinct values, you can use the distinct() and count() functions:
count = Project.objects.values('informationunit__username').distinct().count()
- [Django]-How do I run tests against a Django data migration?
- [Django]-Django datefield filter by weekday/weekend
- [Django]-Get count of related model efficiently in Django
- [Django]-Django Rest Framework pagination extremely slow count
- [Django]-Django-debug-toolbar not showing up
- [Django]-Changing a project name in django
9👍
SQL SELECT field1, COUNT(DISTINCT(pk)) FROM project GROUP BY field1 ORDER BY NULL;
QuerySet
Project.objects.all().values(field1).annotate(count=Count('pk', distinct=True)).order_by()
- [Django]-Django form: what is the best way to modify posted data before validating?
- [Django]-How to get a favicon to show up in my django app?
- [Django]-How to define two fields "unique" as couple
Source:stackexchange.com