我有一个名为测验的模型。每个测验在模型Quiz_Question中都有一组相关的问题。
测验模型: -
class Quiz(models.Model):
quiz_name = models.CharField(max_length=200)
Quiz_Question模型: -
class Quiz_Question(models.Model):
quiz = models.ForeignKey(Quiz, related_name='questions')
text = models.CharField(max_length=200)
现在,我可以使用related_name属性获取特定测验的所有问题,如下所示: -
all_quizes = Quiz.objects.all()
一个特别的测验: -
quiz = all_quizes[0]
与此测验相关的所有问题如下: -
all_related_questions = quiz.questions.all()
我有另一个模型作为LearnerQuestionAnswer,其中学习者是普通的django用户: -
class LearnerQuestionAnswer(models.Model):
quiz_question = models.ForeignKey(Quiz_Question)
learner = models.ForeignKey(User)
我想要做的是找出与该用户的特定测验相关的所有问题。
答案 0 :(得分:1)
尝试按Quiz_Question
和quiz
字段过滤learnerquestionanswer__learner
:
quiz = all_quizes[0]
learner= User.objects.get(id=1)
Quiz_Question.objects.filter(quiz=quiz, learnerquestionanswer__learner=learner)