FastAPI How to control fields in response based on user query parameter e.g fieldSet in FastAPI?

Viewed 495

Hello All I am new to rest api and trying to write a get endpoint to which user send a query parameter say FieldSet. Based on values in FieldSet I want to return response. For Example : Endpoint : @app.get(/blogs/) Blogs endpoint has say 100 fields. But user want to control which fields he/she may need in response. User will hit URL as /blogs/?fieldSet=blogID,blogCreator.

So response only should contain two feilds.

Options

  1. Is it possible with FastAPI's pyndantic response schema ? Can schema be created dynamically based on query parameter values ?
  2. Should I create a a sql based on fieldSet values at runtime and create a response ?
  3. if second option holds true , can we use SqlAlchemy or should I write raw SQLs against DB ?
  4. Also, How to create nested json if one blog may have one or more creators ?
0 Answers
Related