简体   繁体   English

我想在django对面查询吗?

[英]I want make a query in django opposing of where?

Eg I have the models 例如我有模型

class Question(models.Model):
    question = models.CharField(max_length="200")

class Answer(models.Model):
    question= models.ForeignKey(Question)

So, I want all Question's that I don't have in Answers 所以,我想要答案中没有的所有问题

Eg In answer I have 例如,我有

Question 1
Question 2

and in Question I have 问题是我有

Question 1
Question 2
Question 3
Question 4

and I want like result of my Query Question 3 and Question 4 我想要查询问题3和问题4的结果

thanks 谢谢

认为您想要的是:

unanswered_questions = Question.objects.filter(answer__isnull=True)

The easiest way is to first get a distinct list of all question IDs in Answer, then get all Questions that do not have one of those IDs 最简单的方法是先获取“答案”中所有问题ID的唯一列表,然后获取所有没有这些ID之一的问题

ids = Answer.objects.all().distinct().values_list('question', Flat=True)
unanswered = Question.objects.all().exclude(pk__in=ids)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM