[Django]-Django distinct not working with Postgres

5👍

Ahh just figured it out.

According to the django docs (https://docs.djangoproject.com/en/1.9/ref/models/querysets/#distinct)

Both distinct & order_by have to be the same.

So this does NOT work:

        .distinct('id')
        .order_by('-rating')

But this WILL work:

        .distinct('id')
        .order_by('id')

And this works best:

        .distinct('rating', 'id')
        .order_by('-rating')

Anything wrong with using option 3?

👤Ycon

Leave a comment