Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django仅获取筛选后的最新对象_Python_Django_Django Models - Fatal编程技术网

Python Django仅获取筛选后的最新对象

Python Django仅获取筛选后的最新对象,python,django,django-models,Python,Django,Django Models,我正在为Django开发一个问卷类型的应用程序。 当参与者完成问卷调查时,将创建一个AnswerSet对象,该对象将其用户对象链接到一个QuestionAnswer对象列表,每个问题对应一个AnswerSet对象 class AnswerSet(models.Model): user=models.ForeignKey(User) questionnaire=models.ForeignKey(Questionnaire) class QuestionAnswer(mode

我正在为Django开发一个问卷类型的应用程序。 当参与者完成问卷调查时,将创建一个AnswerSet对象,该对象将其用户对象链接到一个QuestionAnswer对象列表,每个问题对应一个AnswerSet对象

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)
应用程序允许人们重新回答问卷,在这种情况下,使用他们现有的答案呈现表单,对于更新的答案,将创建并保存一个新的QuestionAnswer对象

因此,我的问题是:


为了在参与者编辑问卷时显示最新的答案,我希望能够获得一个答案集,然后过滤问题答案,以便为每个问题获得一个问题答案,如果任何问题都有多个问题答案,我只想看看最近的一个

多亏了@Rohan的建议和这篇文章,我找到了一个适合我的解决方案,希望其他人也会觉得它有用! 我添加了一个新模型:

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)
并覆盖了my QuestionAnswerModel上的save函数,因此它看起来像:

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()
现在,每当我需要获得所有最新问题答案的列表时,我可以这样做:

most_recent_answers = [record.question_answer for record in LatestQuestionAnswer.objects.filter(answer_set=<an_answer_set>)]  
most\u-recent\u-answers=[record.question\u在LatestQuestionAnswer.objects.filter(answer\u-set=)中记录的答案]

您的表中是否有将插入时间作为一列的表?是否还要存储以前的答案?如果没有,那么当您为
问题答案
创建模型表单时,请使用现有实例,以便在保存表单时更新该实例。不,我没有插入时间,但可以轻松添加,我正在考虑使用主键确定最新的答案@Rohan是的,我想保留答案的审计跟踪。@Ctrlspc ok,那么它很容易通过
latest(您的\u datetime\u字段)
进行筛选。依赖PK测序是不好的。我怀疑你可能不得不改变你的模型,这个问题可能会有帮助