django左外部联接过滤器不匹配的记录

时间:2019-06-20 03:46:42

标签: django

目标是找出“调查答复”是否没有针对所有顶级问题的最新答案。

这可以通过获取响应的所有顶级问题,然后过滤掉没有当前答案的问题来完成。

我可以用SQL编写此代码,但是有什么方法可以使用Django的QuerySet接口实现呢?

模型

class Survey(Model):
  ...

class SurveySection(Model):
  survey = ForeignKey(Survey, related_name='survey_sections')

class SurveyQuestion(Model):
  survey_section = ForeignKey(SurveySection, related_name='survey_questions')
  parent = ForeignKey('self') #questions can be nested

class SurveyResponse(Model):
  survey = ForeignKey(Survey, related_name='survey_responses')

class SurveyAnswer(Model):
  survey_response = ForeignKey(SurveyResponse, related_name='survey_answers')
  survey_question = ForeignKey(SurveyQuestion, related_name='survey_answer')
  is_current = BooleanField()

SQL

这应该找到响应所针对的调查的所有顶级问题,获取与那些问题匹配的当前答案,并删除没有答案的问题。

select * from survey_surveyquestion question
join survey_surveysection section on section.id = question.survey_section_id
join survey_survey survey on survey.id = section.survey_id
join survey_surveyresponse response on response.survey_id = survey.id
left outer join survey_surveyanswer answer on answer.survey_question_id = question.id and answer.is_current = true
where response.id = 16
  and answer.id is null
  and question.parent is null

1 个答案:

答案 0 :(得分:1)

您可以采用这种方法:

首先,获取父级为空的问题数:

top_question_count = SurveyQuestion.objects.filter(parent__is_null=True).count()

然后,在过滤器中使用它:

from django.db.models imprt Count

SurveyResponse.objects.filter(
    survey_answers__survey_question__parent__is_null=True,
    is_current=True
).annotate(
    top_level_questions=Count('survey_answers__survey_question')
).filter(
    top_level_questions=top_question_count
)