Skip to content

Instantly share code, notes, and snippets.

@stephenmelrose
Last active November 11, 2015 15:01
Show Gist options
  • Save stephenmelrose/56d2b317e446545794cc to your computer and use it in GitHub Desktop.
Save stephenmelrose/56d2b317e446545794cc to your computer and use it in GitHub Desktop.
Dynamic peewee Expression
'''
Original quesion asked on twitter:
> Anyone out there a bit of a peewee guru? I want to via code add N clauses
> to a "WHERE (A=1 AND B=2) OR (A=1 AND B=2) OR ... etc" type query?
For anyone curious, my solution was as follows,
'''
expressions = None
for cycle in cycles:
expression = ((Model.field1 == cycle['val1']) & (Model.field1 == cycle['val2']))
expressions = expressions | expression if expressions else expression
result = Model.select().where(expressions)
@stephenmelrose
Copy link
Author

Pretty simple actually. Brain clearly wasn't working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment