Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8513785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:40:43+00:00 2026-06-11T04:40:43+00:00

I am working on a questionnaire type app for Django. When participants complete a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:40:44+00:00Added an answer on June 11, 2026 at 4:40 am

    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:

    class LatestQuestionAnswer(models.Model):
        question = models.ForeignKey(Question)
        question_answer = models.ForeignKey(QuestionAnswer)
        answer_set = models.ForeignKey(AnswerSet)
        created = created = models.DateTimeField(auto_now_add=True)
    

    And overridden the save function on my QuestionAnswerModel so it looks like:

    def save(self, force_insert=False, force_update=False, using=None):
            super(QuestionAnswer, self).save(force_insert=force_insert, force_update=force_update, using=using)
    
            try:
                record = LatestQuestionAnswer.objects.get(question=self.question, answer_set=self.answer_set)
    
                if record.question_answer == self:
                    return#nothing to do no point updating the record as it is already correct
    
            except ObjectDoesNotExist:
                record = LatestQuestionAnswer(question=self.question, answer_set= self.answer_set)
    
            record.question_answer = self
            record.save()
    

    Now whenever I need to get a list of all the most recent QuestionAnswers I can do something like this:

    most_recent_answers = [record.question_answer for record in LatestQuestionAnswer.objects.filter(answer_set=<an_answer_set>)]  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a questionnaire type application in which questions are stored in a
I am working in a project in which user fills a questionnaire and the
Working with a Django app. I have a List of ads and I want
I am new to moodle ,currently i am working on questionnaire module, in which
I'm working on a .NET-powered questionnaire which contains several repeater controls, each row of
Working with Django 1.2 I am making a wine review site. A user should
I am working on a java questionnaire app, and there is one problem: for
Working on a questionnaire app for cognitively challenged users. Various levels of autism, different
Working in an app where a page_title method was defined in application_helper.rb as such:
I am working on an iPhone application which is crashing after the routine tied

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.