python - Django,查询相关字段: related field exists which meet conditions

标签 python django django-queryset

假设您有问答模型。

我希望问题的答案既不会被删除,也不会被审查。
(我想要以下函数返回 True 的问题。)

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)

关于python - Django,查询相关字段: related field exists which meet conditions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27699149/

相关文章:

python - 如何结合 LSTM 和 CNN 进行时间序列分类

django - 在 Django 中链接多个 filter(),这是一个错误吗?

django - 如果是 radio 检查,HTMX 不会触发加载

Django)使用多对多关系 View 中的查询集过滤

python - 如何在 django 查询过滤器中进行类型转换?

python - 如何选择我在 Linux 上运行的 python 版本?

python - 计算 pandas 中每行具有某些值的列数

python - makemessages 命令导致 html.py 文件和 UnicodeDecodeError

python - 在与 Django 的 session 中存储一个查询集

python - 为什么django不删除一对一关系?