开发者

How do I write this join in django?

开发者 https://www.devze.com 2023-02-18 18:47 出处:网络
Look at this django code: filtered = Question.objects.filter(children__marked=True).distinct() return Question.objects.exclude(id__in=filtered)

Look at this django code:

filtered = Question.objects.filter(children__marked=True).distinct()
return Question.objects.exclude(id__in=filtered)

It returns the list of开发者_开发问答 all questions, except those that have an accepted (marked) answer. I'm trying to make it return questions with upvoted answers as well. See this SQL join:

SELECT * from forum_node AS questions

JOIN forum_node AS answers
ON
questions.node_type = 'question' AND
answers.node_type = 'answer' AND
answers.parent_id = questions.id

WHERE
answers.score > 0 or answers.marked

Now, how do I translate this join into Django?


answered = Question.objects.filter(children__marked=True).distinct()
unanswered = Question.objects.exclude(id__in=answered) 
upvoted = Question.objects.filter(children__score__gt=0).distinct()
return unanswered | upvoted
0

精彩评论

暂无评论...
验证码 换一张
取 消