美文网首页
常用的filter操作符

常用的filter操作符

作者: 空口言_1d2e | 来源:发表于2019-06-17 12:17 被阅读0次

    下面的这些操作符可以应用在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'))
    

    相关文章

      网友评论

          本文标题:常用的filter操作符

          本文链接:https://www.haomeiwen.com/subject/xbrofctx.html