简体   繁体   English

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

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

I'm using the (awesome) Python Peewee ORM for my Flask project, but I now got stuck trying to do a query with a where value in ['a', 'b', 'c'] . 我正在为我的Flask项目使用(真棒)Python Peewee ORM,但我现在卡住了尝试使用where value in ['a', 'b', 'c']where value in ['a', 'b', 'c']查询。 I tried doing it as follows: 我尝试按如下方式进行:

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

But unfortunately it returns all records in the DB. 但遗憾的是,它会返回数据库中的所有记录。 Any ideas how I could do this? 我有什么想法可以做到这一点?

The docs has the answer: x << y will perform x IN y, where y is a list or query . 文档的答案是: x << y将执行x IN y,其中y是列表或查询 So the final query will look like: 所以最终的查询将如下所示:

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

You can also do "IN" expressions with a subquery. 您还可以使用子查询执行“IN”表达式。 For example, to get users whose username starts with "a": 例如,要获取用户名以“a”开头的用户:

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

The .in_() method signifies an "IN" query .in_()方法表示“IN”查询

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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