Last active
November 11, 2015 15:01
-
-
Save stephenmelrose/56d2b317e446545794cc to your computer and use it in GitHub Desktop.
Dynamic peewee Expression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pretty simple actually. Brain clearly wasn't working.