Django queryset中的多重排除

时间:2014-10-01 13:20:13

标签: django django-queryset

我在django queryset中使用排除链,但它没有被正确排除。

skipped_questions = QuestionSkipped.objects.filter(user=request.user)
Question.objects.exclude(Q(id__in=skipped_questions) | Q(created_by=request.user))

两个Q参数单独正常工作,但当我将它们与OR连接时,它只会过滤Q(created_by=request.user)

我也在下面尝试过,但也没有成功。

Question.objects.exclude(id__in=skipped_questions, created_by=request.user)
Question.objects.exclude(id__in=skipped_questions).exclude(created_by=request.user)

在这种情况下使用多重排除的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

尝试这个

skipped_questions = QuestionSkipped.objects.filter(user=request.user).values_list('id', flat=True)
ques = Question.objects.exclude(id__in=skipped_questions) 
ques = ques.exclude(created_by=request.user)