【发布时间】:2015-06-29 10:04:01
【问题描述】:
所以我有 3 个模型
class User(models.Model):
class UserQuestions(models.Model):
user = models.ForeignKey(User)
question = models.ForeignKey(Question)
class UserAnswers(models.Model):
user = models.ForeignKey(User)
question = models.ForeignKey(Question)
我想获取所有用户的列表,但我想通过问题的字段过滤他们的 userquestion_set 和 useranswer_set,然后用 userquestions_set 和 useranswer_set 的总和对查询集进行注释。
如果我这样做:
User.objects.filter(Q(userquestions__question_field=value)&Q(useranswers__question_field=value)).annotate
它过滤我的用户,我想过滤他们的用户问题和用户答案集,以便它们只包含该字段与我的值相等的问题,以便为查询集中的行获得适当的注释。
非常感谢任何帮助。
【问题讨论】:
-
UserQuestions.objects.filter(question__field=value)有什么问题? -
我修改了我的问题并添加了更多信息。基本上我需要带有注释的用户,这就是为什么我不能从其他 2 个模型中做到这一点。获取每个用户并为每个用户过滤两次并制作列表也是不可能的,因为它需要太多并且对数据库进行太多查询。
标签: python django filter django-queryset