0

I have an app that saves Student quizzes using two models: StudentQuiz saves the questions the student was asked StudentQuestion saves the responses of the student for each of those questions.

class StudentQuestion(models.Model):
    user = models.ForeignKey(User)
    question = models.ForeignKey('Question')  # comes from a Questions table
    answer = models.CharField("Answer", max_length = 100, blank=True, null=True)
    q_score = models.IntegerField("Score", blank=True, null=True)


class StudentQuiz(models.Model):
    user = models.ForeignKey(User)
    date = models.DateField("Quiz Date", blank=True, null=True)
    total_score = models.IntegerField("Score", blank=True, null=True)
    ques1 = models.ForeignKey(StudentQuestion, related_name='q1')
    ques2 = models.ForeignKey(StudentQuestion, related_name='q2')
    ques3 = models.ForeignKey(StudentQuestion, related_name='q3')
    ques4 = models.ForeignKey(StudentQuestion, related_name='q4')
    ques5 = models.ForeignKey(StudentQuestion, related_name='q5')

I want to find the number of questions a student took in a certain date range for which he got a score of, say, 1.

So I create the first queryset:

quizzes_done = StudentQuiz(user=mystudent, date__gte=start_date, date__lte=end_date)

Now, I want to look at all questions that are in these StudentQuizzes and want to count the number of questions that have q_score = 1.

Currently, I am just looping over the QuerySet and doing this programmatically. But the QuerySet could be huge.

Is there a way to do this using django's DB APIs?

cezar
  • 11,616
  • 6
  • 48
  • 84
zaphod
  • 2,045
  • 1
  • 14
  • 18

1 Answers1

2

take a look at the docs on queries that span relationships

basically, you should just be able to reference the student_quiz associated with your StudentQuestion object in a query on StudentQestion, filtering on q_score and user, and then use __ to access the StudentQuiz properties you want (e.g. filter(student_quiz__date_gte=blah)

Colleen
  • 23,899
  • 12
  • 45
  • 75
  • I have been trying your suggestion, but do not get it yet. I have a QuerySet of StudentQuizzes. Each StudentQuiz has 5 FKs to StudentQuestion. While StudentQuestion has no FK to StudentQuiz. So I cannot filter on StudentQuestion and look for the student_quiz__date_gte=blah -- right? – zaphod Jul 18 '13 at 20:25
  • you actually can! From the docs "It works backwards, too. To refer to a “reverse” relationship, just use the lowercase name of the model." If you look at your example, it's similar to your setup-- Blog to Entry is a one to many relationship, where Entries have foreign keys to blogs. https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships is another good section on that page (though if I were you I might read from the beginning) – Colleen Jul 19 '13 at 17:41
  • I did read the reverse relationship. I do not get all the entries. I guess that the part where I am probably going wrong is that there are 5 FKs to the same model. So probably just using model name is not enough? – zaphod Jul 24 '13 at 22:27