I am working on a questionnaire type app for Django.
When participants complete a questionnaire an AnswerSet object is created which links their user object to a list of QuestionAnswer object, one for each question that they answer.
class AnswerSet(models.Model):
user=models.ForeignKey(User)
questionnaire=models.ForeignKey(Questionnaire)
class QuestionAnswer(models.Model):
question = models.ForeignKey(Question)
answer = models.CharField(max_length=255)
answer_set = models.ForeignKey(AnswerSet)
The app allows people to re-answer a questionnaire, and in this case renders the form using their existing answers, and for answers that are updated, a new QuestionAnswer object is created and saved.
My question therefore is this:
In order to display the most recent answer when the participant is editing the questionnaire, I want to be able to get an AnswerSet, and then filter the QuestionAnswers so that I get a QuestionAnswer for each question, and where there are more than one QuestionAnswer for any question, I only want to see the most recent one
Thanks to the suggestion by @Rohan and this post Django query select distinct by field pairs I have found a solution that works for me, hopefully someone else will find it useful too!
I have added a new model:
And overridden the save function on my QuestionAnswerModel so it looks like:
Now whenever I need to get a list of all the most recent QuestionAnswers I can do something like this: