I am building a Quiz Application. I have a simple queryset, where I want to filter all the right answers given by the user. So far so good. After that, I would like to obtain the results, divided per subject, si I am trying to figure a way to filter even more. I post some code for better understanding:
models.py
class QuestionDraft(models.Model):
quiz_profile = models.ForeignKey(QuizProfile,
on_delete=models.CASCADE,
blank=True,
null=True)
question = models.ForeignKey(Question,
on_delete=models.CASCADE,
blank=True,
null=True)
text = models.TextField()
is_answered = models.BooleanField(blank=True, null=True)
is_correct = models.BooleanField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Question(models.Model):
id = models.CharField(max_length=7,
default=generate_question_id,
unique=True,
primary_key=True,
editable=False)
question_subject = models.ForeignKey(
QuestionSubject, on_delete=models.CASCADE)
text = models.TextField()
mark = models.IntegerField(default=1)
is_published = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class QuestionSubject(models.Model):
quiz_course = models.ForeignKey(QuizTheoryCourse, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
exam_questions_num = models.IntegerField(null=True, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
In my views I am trying, initially to filter the correct answers (which I am getting right) and after that to group the right answers per subject. Let's say, I have 10 different subjects
views.py
def test_report(request, pk):
quiz_profile = QuizProfile.objects.get(id=pk)
question_drafts = QuestionDraft.objects.filter(quiz_profile=quiz_profile)
total_questions = question_drafts.count()
right_answers = quiz_profile.right_answer
quiz_percentage = ((right_answers * 100) / total_questions)
wrong_answers = total_questions - quiz_profile.right_answer
right_ans = QuestionDraft.objects.filter(
quiz_profile=quiz_profile, is_correct=True)
subjects = [
question.question.question_subject for question in right_ans]
right_ans_list = []
for s in subjects:
print(right_ans.filter(question__question_subject=s).count())
context = {
'quiz_profile': quiz_profile,
'total_questions': total_questions,
'quiz_percentage': quiz_percentage,
'wrong_answers': wrong_answers,
'right_answers': right_answers,
'subjects': subjects,
}
return render(request, 'quiz/test_report.html', context)
I tried many different ways but I couldn't find a proper way how to loop through or filter even more.
CodePudding user response:
Can you try this? this should give you subjects for Specific Quiz and question count which have been answered.
questions = QuestionDraft.objects.filter(quiz_profile_id=quiz_profile, is_answered=True).annotate(que_count=Count('question__question_subject_id')).values("question__question_subject__name","que_count")