7

我正在建立一个 Django 站点进行讨论。用户可以参与讨论,也可以对讨论和讨论中的消息投赞成票。一个简化的数据模型如下:

class Discussion:
    name = models.CharField(max_length=255)

class Message:
    owner = models.ForeignKey(User, related_name='messages')
    body = models.TextField()
    discussion = models.ForeignKey(Discussion, related_name='messages')

class MessageApprovalVote:
    owner = models.ForeignKey(User, related_name='message_approval_votes')
    message = models.ForeignKey(Message, related_name='approval_votes')

class DiscussionApprovalVote:
    owner = models.ForeignKey(User, related_name='discussion_approval_votes')
    discussion = models.ForeignKey(Discussion, related_name='approval_votes')

我想选择前 20 个“最活跃”的讨论,这意味着按该讨论的消息数、消息批准投票总数和讨论批准投票数的总和排序,或者(在伪代码中):

# Doesn't work
Discussion.objects.
    order_by(Count('messages') + 
             Count('approval_votes') + 
             Count('messages__approval_votes'))

使用注释,我可以计算三个评分因素的总和:

scores = Discussion.objects.annotate(
    total_messages=Count('messages', distinct=True),
    total_discussion_approval_votes=Count('approval_votes', distinct=True),
    total_message_approval_votes=Count('messages__approval_votes', distinct=True))

然后,当我找到extra方法时,我以为我正在做某事:

total_scores = scores.extra(
    select={
        'score_total': 'total_messages + total_discussion_approval_votes + total_message_approval_votes'
    }
)

然后就可以做到:

final_answer = total_scores.order_by('-score_total')[:20]

extra电话给出了一个DatabaseError

DatabaseError: column "total_messages" does not exist
LINE 1: SELECT (total_votes + total_messages + total_persuasions) AS...

因此我被挫败了。该extra方法不能引用annotated 字段吗?除了使用原始 sql 查询之外,还有其他方法可以做我想做的事情吗?如果这有所作为,我正在使用 Postgres。

任何见解将不胜感激!

4

2 回答 2

4

我认为这在单个顶级 SQL 查询中是不可能的。score_total 值取决于三个聚合结果,但您要求同时计算它们。

在直接 SQL 中,您可以使用子查询来执行此操作,但我不确定如何将其注入 Django。在使用您的模型设置一个简单的 Django 应用程序之后,以下查询似乎对 SQLite 数据库起到了作用:

SELECT  id, name,
    total_messages, total_discussion_approval_votes, total_message_approval_votes,
    (total_messages +
     total_discussion_approval_votes +
     total_message_approval_votes) as score_total
FROM
   (SELECT
    discussion.id,
    discussion.name,
    COUNT(DISTINCT discussionapprovalvote.id) AS total_discussion_approval_votes,
    COUNT(DISTINCT messageapprovalvote.id) AS total_message_approval_votes,
    COUNT(DISTINCT message.id) AS total_messages
    FROM discussion
    LEFT OUTER JOIN discussionapprovalvote
         ON (discussion.id = discussionapprovalvote.discussion_id)
    LEFT OUTER JOIN message
         ON (discussion.id = message.discussion_id)
    LEFT OUTER JOIN messageapprovalvote
         ON (message.id = messageapprovalvote.message_id)
    GROUP BY discussion.id, discussion.name)

ORDER BY score_total DESC
LIMIT 20;
于 2013-04-05T01:21:03.980 回答
0

实际上有一种方法可以使用带有F 表达式的额外注释:

Discussion.objects.annotate(
    total_messages=Count('messages', distinct=True),
    total_discussion_approval_votes=Count('approval_votes', distinct=True),
    total_message_approval_votes=Count('messages__approval_votes', distinct=True)),
    total_score=F('total_messages') + F('total_discussion_approval_votes') + F('total_message_approval_votes')
).order_by('total_score')
于 2016-03-31T16:00:00.290 回答