Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLALCHEMY or_ list comprehension

I'm trying to use list comprehension with or_. I already found a post that showed how to do it with only checking one column, but I'm unsure how to check two columns simultaneously with a list. I tried something like this:

q = q.filter(or_((model.column.contains(word), model.column2.contains(word))for word in search))

This gives me a bad request though. Any advice on how to search simultaneously using a list would be appreciated!

like image 583
User Avatar asked Sep 13 '25 00:09

User


1 Answers

From what I understand you need to unpack the conditions into positional arguments of or_():

columns = [model.column, model.column2]
conditions = [column.contains(word) for word in search for column in columns]
q = q.filter(or_(*conditions))

Assuming you want to "or" every contains for every word and for all given columns.

like image 54
alecxe Avatar answered Sep 15 '25 14:09

alecxe