Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python peewee dynamically or clauses

Tags:

python

peewee

I want to dynamically OR multiple clauses when performing a query. I see in the peewee documentation that:

import operator
or_clauses = reduce(operator.or_, clauses)  # OR together all clauses

However, this note is somewhat unclear. What exactly is clauses supposed to be set to? Does anyone have any example code?

like image 533
Atul Bhatia Avatar asked Dec 15 '22 00:12

Atul Bhatia


1 Answers

clauses would be a list of expressions in the example, sorry that it is unclear.

You might write something like:

clauses = [
    (User.username == 'something'),
    (User.something == 'another thing'),
    ...
]
User.select().where(reduce(operator.or_, clauses))
like image 120
coleifer Avatar answered Jan 02 '23 13:01

coleifer