【问题标题】:Peewee select where(True or (True and True)) is not workingPeewee 选择 where(True or (True and True)) 不起作用
【发布时间】:2021-11-24 16:27:39
【问题描述】:

我正在与 Peewee 合作挑选书籍。我想根据类型选择书籍。 在搜索或查看 peewee 的说明时,我没有找到任何帮助。

请参阅下面的示例,其中我希望选择所有无效的 PocketHard_coverthreaded 书籍:

Books.select(
    ).where(
        Book.title.contains("Some title")
    ).where(
        Book.type == "Pocket" |
        (
            Book.type == "Hard_cover" &
            Book.binding == "threaded"
        )
    )

选择Pocket 有效,但不会选择任何Hard_coverthreaded

注意:我试过在 else | 后面没有 ()然后什么都不选。

【问题讨论】:

  • 我还没有找到查询的解决方案。我要做的是将调用包装在一个接口中,该接口执行 2 个数据库调用并返回一个包含找到的书籍的列表。

标签: python sqlite peewee


【解决方案1】:

由于 PYthon 运算符优先级,您需要括号:

).where(
    (Book.type == "Pocket") |
    (
        (Book.type == "Hard_cover") &
        (Book.binding == "threaded")
    )
)

文档清晰:http://docs.peewee-orm.com/en/latest/peewee/query_operators.html

【讨论】:

    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 2020-12-15
    • 2018-02-24
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多