常用的filter操作符
2019-06-17 本文已影响0人
空口言_1d2e
下面的这些操作符可以应用在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'))