基于条件的Concat Queryset?

问题描述 投票:0回答:1

假设我有像QuestionAnswer这样的模特

class Question(models.Model):
     category = models.ForeignKey(Category)

#contains all the choices of questions
class Options(models.Model):
     question = models.Foreignkey(Question)

#contains question, choice choosen by user, category
class Answer(models.Model):
     question = models.Foreignkey(Question)
     option = models.ForeignKey(Option)
     category = models.ForeignKey(Category)

我的查询集如下

questions = Question.objects.filter(category_id=1)
answers = Answer.objects.filter(category_id=1)

questions查询集具有类别的所有问题,answers查询集具有每个user的问题的所有答案。现在我想显示所有问题的答案和警告信息到未答复的问题,实现这一目标的最佳方法是什么?

django django-models django-queryset django-1.11
1个回答
0
投票

你可以做点什么

>>> answered_question = Answer.objects.filter(question__isnull = False).values('question')
>>> Question.objects.exclude(pk__in = answered_question)
© www.soinside.com 2019 - 2024. All rights reserved.