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 `

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.