SQLAlchemy search using varying keywords
It should be possible to create a list of you like filters and pass them all to and_
.
First create a list of the like queries:
search_queries = search_query.split(',')filter = [Artikel.Benennung.like("%"+sq"%") for sq in search_queries]
Then pass them to and_
, unpacking the list:
and_( Artikel.Wg == selectet_wg, *filter )
*filter
has to be the last argument to and_
otherwise python will give you an error.
You can call filter multiple times:
search_query = request.form["searchinput"]if selectet_wg and not lagernd: query = db_session.query( Artikel.Artnr, Artikel.Benennung, Artikel.Bestand, Artikel.Vkpreisbr1 ).filter(Artikel.Wg == selectet_wg) for prop in search_query.split(','): query = query.filter(Artikel.Benennung.like("%"+prop+"%")) query = query.order_by(Artikel.Vkpreisbr1.asc())