簡體   English   中英

如何在Python Peewee ORM中使用`WHERE value IN list`進行查詢?

[英]How to do query with `WHERE value IN list` in the Python Peewee ORM?

我正在為我的Flask項目使用(真棒)Python Peewee ORM,但我現在卡住了嘗試使用where value in ['a', 'b', 'c']where value in ['a', 'b', 'c']查詢。 我嘗試按如下方式進行:

MyModel.select().where(MyModel.sell_currency in ['BTC', 'LTC'])

但遺憾的是,它會返回數據庫中的所有記錄。 我有什么想法可以做到這一點?

文檔的答案是: x << y將執行x IN y,其中y是列表或查詢 所以最終的查詢將如下所示:

MyModel.select().where(MyModel.sell_currency << ['BTC', 'LTC'])

您還可以使用子查詢執行“IN”表達式。 例如,要獲取用戶名以“a”開頭的用戶:

a_users = User.select().where(fn.Lower(fn.Substr(User.username, 1, 1)) == 'a')

.in_()方法表示“IN”查詢

a_user_tweets = Tweet.select().where(Tweet.user.in_(a_users))

http://peewee.readthedocs.io/en/latest/peewee/query_operators.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM