0

I have used django to develop a web app. I want to get the distinct "title" form the queryset get by filter.

But I use mysql so could not pass "title" to distict. How could I filter the queryset with the distinct "title"?

query_set = CourseInfo.objects.filter(discipline_id=id).distinct('title')

return render(request, 'main.html',
                     context={'query_set':query_set})

I get error for this in mysql as it may only used in postgresql `

django
  • 13
  • 6

1 Answers1

0

It will give you distinct titles:

titles = CourseInfo.objects.filter(
    discipline_id=id
).order_by('title').values('title').distinct()  

Note:
there is no such thing called SELECT DISTINCT ON in MySQL.
You can only use it in Postgresql but maybe GROUP BY helps you for converting SELECT DISTINCT ON query to MySQL query.
Check out this link then you kinda can convert this query to MySQL query.

Mojtaba Arezoomand
  • 2,140
  • 8
  • 23