i want to use django models feature to excute this sql query.
SELECT COUNT(DISTINCT ques_id), title FROM contest_assignment WHERE grp_id = 60 GROUP BY title;
i tried this but it did not give me proper result:
from django.db.models import Count assignment.objects.values_list('title').annotate(count=Count('ques')).values('title', 'count')
how can i use django model?
Best How To :
You shouldn't use both
count field is implicitly added to the set of fields that is returned.
Django supports a
COUNT(DISTINCT <field>) query by using
.annotate(count=Count('<field>', distinct=True)). That would leave you with the following query: