如何在多个 SQLAlchemy 模型中进行一般 "search bar" 搜索
How to do a general "search bar" search across multiple SQLAlchemy models
我的应用程序有一个搜索栏。假设用户搜索 'John Smith United Kingdom Oxford University' 我想 return 来自帐户 table 的用户 name
'John Smith', location
'United Kingdom' 和 education
'Oxford University'.
这里是搜索的一般概述 'John Smith United Kingdom Oxford University':
- 使用
name
'John Smith'、location
'United Kingdom' 和 education
'Oxford University'. 显示数据库中的帐户
- 显示
name
'John Smith' 和 education
'Oxford University' 的帐户
- 显示
name
'John Smith' 和 location
'United Kingdom' 的帐户
- 显示
name
'John Smith' 的帐户
我找不到任何专门针对 Flask-SQLAlchemy 的资源来指定如何执行此操作。我知道我必须使用 .like()
虽然我不知道如何将它添加到 Flask 中的 SQLAlchemy 查询中。使用 SQLAlchemy 我可以做 .where(column.like())
尽管我不能在 Flask-SQLAlchemy 中做。
我该怎么做?
您使用 like()
就像在 SQLAlchemy
中使用它一样:
User.query.filter(User.name.like("%John Smith%")).all()
或者您可以使用 contains()
:
User.query.filter(User.name.contains('John Smith')).all()
你最好用ilike
User.query.filter(User.name.ilike("%John%")).all()
我的应用程序有一个搜索栏。假设用户搜索 'John Smith United Kingdom Oxford University' 我想 return 来自帐户 table 的用户 name
'John Smith', location
'United Kingdom' 和 education
'Oxford University'.
这里是搜索的一般概述 'John Smith United Kingdom Oxford University':
- 使用
name
'John Smith'、location
'United Kingdom' 和education
'Oxford University'. 显示数据库中的帐户
- 显示
name
'John Smith' 和education
'Oxford University' 的帐户
- 显示
name
'John Smith' 和location
'United Kingdom' 的帐户
- 显示
name
'John Smith' 的帐户
我找不到任何专门针对 Flask-SQLAlchemy 的资源来指定如何执行此操作。我知道我必须使用 .like()
虽然我不知道如何将它添加到 Flask 中的 SQLAlchemy 查询中。使用 SQLAlchemy 我可以做 .where(column.like())
尽管我不能在 Flask-SQLAlchemy 中做。
我该怎么做?
您使用 like()
就像在 SQLAlchemy
中使用它一样:
User.query.filter(User.name.like("%John Smith%")).all()
或者您可以使用 contains()
:
User.query.filter(User.name.contains('John Smith')).all()
你最好用ilike
User.query.filter(User.name.ilike("%John%")).all()