flask 多条件查询

筛选的时候会遇到多个筛选条件,但都是非必填的
把存在的筛选条件添加到数组中,然后进行解包
最后把解包后的数据添加到筛选条件中

@api.route("/role/list")
def roleList():
    name = request.args.get("name")
    gender = request.args.get("gender")
    attribute = request.args.get("attribute")
    site = request.args.get("site")

    filterList = []

    if name is not None:
        filterList.append(Role.name.like('%'+name+'%'))
    if gender is not None:
        filterList.append(Role.gender == gender)
    if attribute is not None:
        filterList.append(Role.attribute == attribute)
    if site is not None:
        filterList.append(Role.site == site)

    role = Role.query.filter(*filterList).all()

    data = []
    for i in role:
        info = {
            "id":i.id,
            "name":i.name,
            "attribute":i.attribute,
            "head_portrait":i.head_portrait
        }
        data.append(info)

    return jsonify(code="1",msg="成功",data=data)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,170评论 1 32
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,426评论 0 3
  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 2,910评论 2 9
  • 这周一直头痛,头重重的,晕晕的......以前痛一下,睡一觉就好了,但这次好几天都没好。想要去医院拍个片子,放心点...
    晓言晓语阅读 113评论 0 0
  • 今天是姥爷的生日。所以,选择今天来写,发表这篇文章。感觉之前也写过,但是比较粗糙,今天希望能认真表达一下情感。 先...
    矛盾的蟹子阅读 455评论 0 0