Python Flask-SQLAlchemy 过滤器和运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21674303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask-SQLAlchemy filters and operators
提问by Matthew
Flask-SQLAlchemy gives the option to filter a query. There are a wide number of ways you can filter a query - the examples the Flask-SQLAlchemy docs give:
Flask-SQLAlchemy 提供了过滤查询的选项。您可以通过多种方式过滤查询 - Flask-SQLAlchemy 文档提供的示例:
User.query.filter_by(username='peter') # Returns all users named 'peter'
User.query.filter(User.email.endswith('@example.com')) # Returns all users with emails ending in '@example.com'
I also found this for one-to-many relationships:
我还发现这适用于一对多关系:
User.query.filter(User.addresses.any(address=address)) # Returns all users who have a particular address listed as one of their addresses
Questions:
问题:
- Does anyone know what filters are actually available to be used? I can't find a list of filters anywhere in the documentation, which makes it rather hard to query databases.
- In particular, what filter would I use to check if a user's email is contained within a particular set of email addresses?
- 有谁知道实际上可以使用哪些过滤器?我在文档的任何地方都找不到过滤器列表,这使得查询数据库变得相当困难。
- 特别是,我将使用什么过滤器来检查用户的电子邮件是否包含在一组特定的电子邮件地址中?
采纳答案by Paolo Casciello
For a list of filters check SQLAlchemy documentation
有关过滤器列表,请查看SQLAlchemy 文档
what filter would I use to check if a user's email is contained within a particular set of email addresses?
我将使用什么过滤器来检查用户的电子邮件是否包含在一组特定的电子邮件地址中?
Columns have a .in_()method to use in query. So something like:
列有一种.in_()在查询中使用的方法。所以像:
res = User.query.filter(User.email.in_(('[email protected]', '[email protected]')))
Hereyou can find the list of column method for expressions.
在这里您可以找到表达式的列方法列表。
回答by PatrickReagan
Updated links:
更新链接:
SQLAlchemy documentation: queries https://docs.sqlalchemy.org/en/latest/orm/query.html
SQLAlchemy 文档:查询 https://docs.sqlalchemy.org/en/latest/orm/query.html
Column elements https://docs.sqlalchemy.org/en/latest/core/sqlelement.html
列元素 https://docs.sqlalchemy.org/en/latest/core/sqlelement.html
Common Filter operators http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#common-filter-operators
常用过滤器运算符 http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#common-filter-operators
Column operators https://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.operators.ColumnOperators
列运算符 https://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.operators.ColumnOperators

