Sometimes, we want to annotate count with a distinct field with Python Django.
In this article, we’ll look at how to annotate count with a distinct field with Python Django.
How to annotate count with a distinct field with Python Django?
To annotate count with a distinct field with Python Django, we can call annotate with the distinct argument set to True in the aggregation function.
For instance, we write
p = Project.objects.all().annotate(Count('informationunit__username',
distinct=True))
to call annotate with the aggregation result returned by Count.
We set distinct to True to annotate count with a distinct field.
Conclusion
To annotate count with a distinct field with Python Django, we can call annotate with the distinct argument set to True in the aggregation function.