我有两个模型,Track
和{Pair
都有一个track1
、track2
和{track1
。以下是我目前所做的尝试:
lstPairs = Pair.objects.order_by('-popularity','track1__id').distinct('track1__id')[:iNumPairs].values_list('track1__id', 'track2__id', 'popularity')
这给了我以下错误:
^{pr2}$……所以我试了一下:
lstPairs = Pair.objects.order_by('-popularity','track1__id').distinct('popularity', 'track1__id')[:iNumPairs].values_list('track1__id', 'track2__id', 'popularity')
这给了我重复的track1__id
s的条目。有人知道解决这个问题的方法吗?我想我将不得不使用raw()
或类似的东西,但我不知道如何处理这样的问题。我使用PostgreSQL作为数据库后端,因此DISTINCT
应该受到支持。在
参见documentation on distinct。在
第一个:
你不能指定什么是你的数据库后端,如果它不是PostrgreSQL你就没有机会让它工作。在
第二:
我认为您应该使用raw(),或者获取按流行程度排序的完整对列表,然后在Python中按track1唯一性进行过滤。在
首先,让我们澄清一下:}是PostgreSQL扩展。在
DISTINCT
是标准SQL,而{错误(
DISTINCT ON expressions must match initial ORDER BY expressions
)表示,您应该修复ORDER BY
子句,而不是DISTINT ON
(如果这样做,您将得到不同的结果,就像您已经经历过的那样)。在这将为您提供预期结果:
在SQL中:
^{pr2}$但可能是顺序错误。
如果您想要原始订单,可以在此处使用子查询:
相关问题 更多 >
编程相关推荐