[Answer]-Getting distinct columns from postgresql in django model

1👍

In django you can use the distinct() method when querying your model.

So something like this

models.YourModel.objects.order_by('degree_code').distinct('degree_code') 

As you are using PostgreSQL note the docs which state

On PostgreSQL only, you can pass positional arguments (*fields) in
order to specify the names of fields to which the DISTINCT should
apply. This translates to a SELECT DISTINCT ON SQL query. Here’s the
difference. For a normal distinct() call, the database compares each
field in each row when determining which rows are distinct. For a
distinct() call with specified field names, the database will only
compare the specified field names.

Leave a comment