django quize App

I need a mentor to teach me how to do quize App in Django or a Learning friend to start together.

Please I need help with django project.
I have this query:

max_q = Result.objects.filter(student_id = OuterRef(‘student_id’), exam_id = OuterRef(‘exam_id’), ).order_by(’-marks’).values(‘id’)

results = Result.objects.filter(id = Subquery(max_q[:1]))

The above query gives me the maximum value of the results, but how can I delete the remaining results? I don’t need them in my database.

You’ve got a good query to start from.

Your subquery is returning the PK of the row you want to keep.

How might you write a query to return all the other results? (Hint, take a look at the exclude method)?

Once you’ve got a result set with the rows to delete, then you use the delete method to delete them from the database.

1 Like

Thank you for the quick response, sir, I don’t know how to get the opposite query. I have trying for more than a week now.

Am tried this:

Results.objects.exclude(Id = result [:1] )
But it deleting everything apart from the maximum expected, but is for all users instead for the

#This is the Result model related to user and exam

class Result(models.Model):

student = models.ForeignKey(Profile,on_delete=models.CASCADE)
exam = models.ForeignKey(Course,on_delete=models.CASCADE)
marks = models.PositiveIntegerField()
date = models.DateTimeField(auto_now=True)
id = models.AutoField(primary_key=True)
def __str__(self):
    return f"{self.student}"

I think I’m going to need to see more of the code. Your previous responses give me a different picture of the situation than where we started.

Specifically, you make a reference to using, but I don’t see any existing reference to it in the query you provided.

So it’s likely to be helpful if you posted the complete view where you’re retrieving data - I’d like to see all the data involved here to provide a suitable answer.

1 Like