假设我有像Question
和Answer
这样的模特
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
的问题的所有答案。现在我想显示所有问题的答案和警告信息到未答复的问题,实现这一目标的最佳方法是什么?
你可以做点什么
>>> answered_question = Answer.objects.filter(question__isnull = False).values('question')
>>> Question.objects.exclude(pk__in = answered_question)