Created
May 12, 2021 13:33
-
-
Save emadflash/0f4030f391f6cb9674b5615b67ea6f00 to your computer and use it in GitHub Desktop.
filter table using json with sqlalchemy, you don't have to say `session.query(Base).filter(Base.foo == "bar", Base.bar == "foo")`
This file contains 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
def create_serach_query(Base, Json: dict) -> list: | |
queries = [] | |
for key, value in Json.items(): | |
queries.append(Base.__dict__[key].like(value)) | |
return queries | |
queries = create_serach_query(Base, query) | |
result = session.query(Base).filter(*queries).all() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment