Skip to content

Instantly share code, notes, and snippets.

@emadflash
Created May 12, 2021 13:33
Show Gist options
  • Save emadflash/0f4030f391f6cb9674b5615b67ea6f00 to your computer and use it in GitHub Desktop.
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")`
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