Case Insensitive Flask-SQLAlchemy Query
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"))
Improving on @plaes's answer, this one will make the query shorter if you specify just the column(s) you need:
user = models.User.query.with_entities(models.User.username).\
filter(models.User.username.ilike("%ganye%")).all()
The above example is very useful in case one needs to use Flask's jsonify for AJAX purposes and then in your javascript access it using data.result:
from flask import jsonify
jsonify(result=user)