【问题标题】:AND OR combinations in Django ORMDjango ORM 中的 AND OR 组合
【发布时间】:2013-06-30 18:44:21
【问题描述】:

我想将这样的查询翻译成 Django ORM:

SELECT * FROM table WHERE status=1 OR (status=3 AND timestamp < ...)

... 是一个有效的时间戳。

我尝试像这样构建Q() 对象:

Q(status=1) | Q (status=3, timestamp__lt=self.timeBeforeStatusIsUpdated)

completedQ = Q(status=1)
pendingQ = Q(status=3) & Q(timestamp__lt=self.timeBeforeStatusIsUpdated)

final = completedQ | pendingQ

两者都只返回带有status=1 的实例。

这个功能在 django 中没有很好的记录(我使用的是最新版本),所以我正在寻求帮助。

【问题讨论】:

    标签: django orm


    【解决方案1】:

    有趣的故事。两个版本都正确且有效。错误在其他地方。

    【讨论】:

      猜你喜欢
      • 2018-06-03
      • 1970-01-01
      • 2011-03-10
      • 2013-05-23
      • 1970-01-01
      • 2011-12-28
      • 2022-01-08
      • 2011-11-05
      • 1970-01-01
      相关资源
      最近更新 更多