flask SQLAlchemy query.filter_by 經常使用操做符

經常使用的filter操做符

下面的這些操做符能夠應用在filter函數中sql

  • 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來源:簡書著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。
相關文章
相關標籤/搜索