I've a StudentAnswer model which stores the answer id given by the student(User) in a quiz. If the answer is correct then 1 marks else 0. I'm also ranking the users based on their marks in a particular quiz.
The model looks like this:
class StudentAnswer(models.Model):
user = models.ForeignKey(User, related_name='user_question_answer',
on_delete=models.SET_NULL,null=True, blank=True)
answer = models.ForeignKey(QuizQuestionAnswer, related_name='user_answer',
on_delete=models.CASCADE)
quiz = models.ForeingKey(Quiz, on_delete=models.CASCADE)
marks = models.IntegerField()
This is the query I'm using to rank my users:
StudentAnswer.objects.filter(
quiz__start_date__month=date.today().month).annotate(
rank=Window(expression=DenseRank(), order_by=[F('marks').desc(),])
)
A user will have multiple entries in table(number of questions he attempted in a month). I want to group by each user, add their marks and then rank them for that particular month and send the response.
How can I do this?