Case Insensitive Flask-SQLAlchemy Query

后端 未结 3 1444
广开言路
广开言路 2020-11-28 06:25

I\'m using Flask-SQLAlchemy to query from a database of users; however, while

user = models.User.query.filter_by(username=\"ganye\").first()
<
3条回答
  •  没有蜡笔的小新
    2020-11-28 07:07

    You can do it by using either the lower or upper functions in your filter:

    from sqlalchemy import func
    user = models.User.query.filter(func.lower(User.username) == func.lower("GaNyE")).first()
    

    Another option is to do searching using ilike instead of like:

    .query.filter(Model.column.ilike("ganye"))
    

提交回复
热议问题