简体   繁体   中英

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'
 }
]

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM