eugene
eugene

Reputation: 41665

Django, query for related field: related field exists which meet conditions

Suppose you have Question and Answer model.

I want questions that have answers which are neither deleted nor being reviewed.
(I want questions that would return True for the following function.)

def has_active_answer(self):
   return self.answers.not_deleted().filter(is_inreview=False).exists()


not_deleted() === filter(deleted=False) # if that makes difference

Upvotes: 2

Views: 312

Answers (1)

falsetru
falsetru

Reputation: 369074

Assuming the model is Question and it has answers field:

Question.objects.filter(answers__deleted=False, answers__is_inreview=False)

Upvotes: 1

Related Questions