I implement FastAPI, SQLAlchemy service that has integration with ERM software. ERM software stores data in table as
Class Table():
main_option_1: Any
main_option_2: Any
class ExtraOption():
extra_option: Any
class TableExtraOption():
table_id: FK
extra_option_id: FK
value: Any
Basically, I did it. But the next step is to get list of Table records. And it is not a big deal. But the problem arises when I try to filter the queryset using query parameters by fields I don’t know and I even don’t know how many fields I have.
Filters are stored as:
class CategoryOfTable():
name: str
class FilterCategory():
option: str (it maybe both main option and extra option)
category_id: FK
order: int (order of that uses frontend to show filter)
In general, it will be good to solve this issue this way because if somebody adds extra options and extra filters in ERM they will upload in my service by event and I won’t add any migrations manually.
I read some questions at stackoverflow and decided to use pydantic schema for query parameters this way:
class ExtraOptionValueQuerySchema(BaseModel):
name: Optional[str] = None
class ExtraOptionQuerySchema(BaseModel):
name: Optional[str] = None
item: Optional[List[ExtraOptionValueQuerySchema]] = Depends()
class RecordQuerySchema(BaseModel):
length_gt: Optional[float] = None
length_lt: Optional[float] = None
width_gt: Optional[float] = None
width_lt: Optional[float] = None
height_gt: Optional[float] = None
height_lt: Optional[float] = None
weight_gt: Optional[float] = None
weight_lt: Optional[float] = None
brand: Optional[str] = None
extra_options: Optional[List[ExtraOptionQuerySchema]] = Depends()
@app.get("")
async def get_records(
category_id: uuid_pkg.UUID, request: Request, query: RecordQuerySchema = Depends(), db: AsyncSession = Depends(get_db)
) -> None:
pass
And I’m not able to check whether it works or not because required kwargs and args appears (exactly kwargs and args that’s the name of query parameters). Is it basically implementable? Do you have any ideas about it?