2016-09-06 23 views
0

我正在嘗試使用or_理解列表。我已經發現了一篇文章,展示瞭如何僅檢查一列,但我不確定如何使用列表同時檢查兩列。我想是這樣的:SQLALCHEMY or_列表理解

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

這給了我一個壞的請求,雖然。任何關於如何使用列表同時搜索的建議將不勝感激!

回答

1

從我的理解,你需要解壓條件進入or_()位置參數:

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

假設你想「或」每一個包含每個單詞和所有給定列。

+1

條件可以是一個生成器:'or_(column.contains(word)for word in search for column in columns)' – RazerM