简体   繁体   中英

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

假设模型是Question ,并且有answers字段:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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