You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello. To start off, I have to admit this library is an amazing time saver so thank you for creating it.
I want to learn about how to define a custom response model at the router level (crud_router). I see that there is the select_schema parameter. However, when I define it to be something like LibraryRead, it is trying to read a relationship field from the database, which does not exists. Take a look at these models as an example (I'm using sqlmodel):
I would then want LibraryRead to be the response model so FastAPI can automatically respond with the appropriate data. With FastAPI, I would define the endpoint like this and the books list would be included in the response:
@router.get("", response_model=LibraryReaddefget_libraries(...):
libraries=get_libraries(...) # Returns a list of Library objectsreturnlibraries# FastAPI automatically returns list of LibraryRead objects because of the response_model
However, when I set the select_schema to LibraryRead, it looks like it is attempting to read books from the database. Which I suppose makes sense but not the functionality I am looking for.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello. To start off, I have to admit this library is an amazing time saver so thank you for creating it.
I want to learn about how to define a custom response model at the router level (
crud_router
). I see that there is theselect_schema
parameter. However, when I define it to be something likeLibraryRead
, it is trying to read a relationship field from the database, which does not exists. Take a look at these models as an example (I'm usingsqlmodel
):I would then want
LibraryRead
to be the response model so FastAPI can automatically respond with the appropriate data. With FastAPI, I would define the endpoint like this and thebooks
list would be included in the response:However, when I set the
select_schema
toLibraryRead
, it looks like it is attempting to readbooks
from the database. Which I suppose makes sense but not the functionality I am looking for.If I don't set
select_schema
then the response, as expected, only includesid
andname
fields.Let me know what I am missing. Thank you!
Beta Was this translation helpful? Give feedback.
All reactions