[Fixed]-Count data from a table excluding duplicates

1👍

If you are using PostgreSQL you can use the following to use DISTINCT ON

Model.objects.order_by('field').dictinct('field').count()

If you are using MySQL you can use

Model.objects.order_by('field').aggregate(count=Count('field', distinct=True))['count']

You need to order_by the field you pass to distinct, there is more information in the docs

Leave a comment