[英]distinct method not working correctly in Django
我想獲取不同的季度和年份對,但以下查詢給出了重復的對
DataValue.objects.values('period_quarter','period_year').distinct()
輸出:
[
{
'period_year': '2019',
'period_quarter': 'Q2'
},
{
'period_year': '2019',
'period_quarter': 'Q2'
},
{
'period_year': '2019',
'period_quarter': 'Q2'
}
]
從文檔中 :
指定字段名稱時,必須在QuerySet中提供order_by(),並且order_by()中的字段必須以相同的順序從distinct()中的字段開始。
嘗試:
DataValue.objects.order_by('period_quarter','period_year').distinct('period_quarter','period_year')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.