sqlmodel
sqlmodel copied to clipboard
How to translate query lookup string into a SQLModel object?
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the SQLModel documentation, with the integrated search.
- [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to SQLModel but to Pydantic.
- [X] I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
# parse selections and generate a lookup query
for sel in selection:
if sel[1] and not sel[1] == "all":
name = sel[0].replace("sel_", "")
val = sel[1]
if name == "customer":
query = query.filter(Xconnect.customer == int(val))
if name == "region":
query = query.filter(Xconnect.region == int(val))
if name == "lvc":
query = query.filter(Xconnect.lvc == str(val))
if name == "id":
query = query.filter(Xconnect.id == int(val))
if name == "conn_status":
query = query.filter(Xconnect.conn_status == int(val))
if name == "environment":
query = query.filter(Xconnect.environment == int(val))
if name == "client_type":
query = query.filter(Xconnect.client_type == int(val))
if name == "vendor":
query = query.filter(Xconnect.vendor == int(val))
if name == "comp_id":
query = query.filter(Xconnect.comp_id == int(val))
Description
Hello, I have a select field to search for DB records based on some fields, table is called Xconnect,
how do I translate a lookup string into a SQLModel object w/o using constant If/Then blocks?
is there a better way to do this than explicit if/then statements?
for example if someone searches Xconnect table where client_type == "abc"
how can I translate this into a SQL model query w/o using explicit if/then mapping, ie
if name == "client_type":
query = query.filter(Xconnect.client_type == int(val))
Operating System
Linux
Operating System Details
Ubuntu 20.04
SQLModel Version
0.0.6
Python Version
3.8
Additional Context
No response