How do I write this join in django?
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
精彩评论