开发者

I want make a query in django opposing of where?

开发者 https://www.devze.com 2023-03-05 00:03 出处:网络
E.g I have the models class Question(models.Model): question = models开发者_运维百科.CharField(max_length=\"200\")

E.g 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

E.g 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

thanks


I think what you want is:

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

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

精彩评论

暂无评论...
验证码 换一张
取 消