2
votes

In my django project i have to implement a groupped query with ORM and order results for a specific field. I do this:

a = tt.objects.filter(thread_status = 'DEAD').select_related().distinct('thread_stag').order_by('-id')[0:20]

but the response is an error:

ProgrammingError: SELECT DISTINCT ON expressions must match initial ORDER BY expressions

How can i do distinct and order_by in same query on different columns?

thanks in advance

1

1 Answers

1
votes

can you try this one? references from

a = tt.objects.filter(thread_status = 'DEAD').select_related()\
              .distinct('thread_stag').order_by('thread_stag', '-id')[0:20]

or

a = tt.objects.filter(thread_status = 'DEAD').select_related()\
              .order_by('thread_stag', '-id').distinct('thread_stag')[0:20]