python peewee dynamically or clauses
Asked Answered
T

1

6

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?

Tiemannite answered 6/3, 2014 at 23:11 Comment(0)
G
15

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))
Garamond answered 7/3, 2014 at 1:41 Comment(1)
Useful example, would be nice to have this in the documentation, unless I missed it. Thank you.Dunedin

© 2022 - 2024 — McMap. All rights reserved.