Shubham
Shubham

Reputation: 531

distinct method not working correctly in Django

I want to get distinct pair of quarter and year but following query is giving duplicate pairs

DataValue.objects.values('period_quarter','period_year').distinct()

output:

[
 {
'period_year': '2019',
'period_quarter': 'Q2'
 },
 {
'period_year': '2019',
'period_quarter': 'Q2'
 },
 {
'period_year': '2019',
'period_quarter': 'Q2'
 }
]

Upvotes: 1

Views: 81

Answers (1)

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14945

From the Docs:

When you specify field names, you must provide an order_by() in the QuerySet, and the fields in order_by() must start with the fields in distinct(), in the same order.

Try:

DataValue.objects.order_by('period_quarter','period_year').distinct('period_quarter','period_year')

Upvotes: 2

Related Questions