sqlalchemy动态组合查询语句。

        if filter_type == 1:
            search = and_(GameRoom.status ==1,or_(
                and_(GameRoom.white_user_id == user_id,
                     GameRoom.active_player == 1),
                and_(GameRoom.black_user_id == user_id,
                     GameRoom.active_player == 0)))
        elif filter_type == 2:
            search = and_(GameRoom.status ==1,or_(
                and_(GameRoom.white_user_id == user_id,
                     GameRoom.active_player == 0),
                and_(GameRoom.black_user_id == user_id,
                     GameRoom.active_player == 1)))
        elif filter_type == 3:
            search = GameRoom.create_by == user_id
        
        db.session.query(GameRoom).filter(search).all()
原文地址:https://www.cnblogs.com/jackadam/p/9536368.html