flask SQLAlchemy query.filter_by 常用操作符
常用的filter操作符
下面的这些操作符可以应用在filter
函数中
equals
:
query.filter(User.name == 'ed')
not equals
:
query.filter(User.name != 'ed')
LIKE
:
query.filter(User.name.like('%ed%'))
IN
:
query.filter(User.name.in_(['ed', 'wendy', 'jack']))
# works with query objects too:
query.filter(User.name.in_(
session.query(User.name).filter(User.name.like('%ed%'))
))
NOT IN
:
query.filter(~User.name.in_(['ed', 'wendy', 'jack']))
IS NULL
:
query.filter(User.name == None)
# alternatively, if pep8/linters are a concern
query.filter(User.name.is_(None))
IS NOT NULL
:
query.filter(User.name != None)
# alternatively, if pep8/linters are a concern
query.filter(User.name.isnot(None))
AND
:
# use and_()
from sqlalchemy import and_
query.filter(and_(User.name == 'ed', User.fullname == 'Ed Jones'))
# or send multiple expressions to .filter()
query.filter(User.name == 'ed', User.fullname == 'Ed Jones')
# or chain multiple filter()/filter_by() calls
query.filter(User.name == 'ed').filter(User.fullname == 'Ed Jones')
OR
:
from sqlalchemy import or_
query.filter(or_(User.name == 'ed', User.name == 'wendy'))
MATCH
:
query.filter(User.name.match('wendy'))
作者:治部少辅
链接:https://www.jianshu.com/p/8d085e2f2657
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted on 2017-12-28 08:11 m0o0mbao 阅读(10634) 评论(0) 编辑 收藏 举报