Hi Sourabh,
I do have another question for you. I'd like you to shed light on the usage of attribute orm_mode=True
within sublass Config
of class ShowBlog
. Leaving aside the fact that Pydantic v2 has discarded orm_mode
in favour of something else, my question is, why do we have to set it considering that in the codebase we are never explicitly transforming SQLAlchemy objects into Pydantic objects?
Indeed, my understanding is that it is needed to provide such transformation given that FastAPI is designed to work with Pydantic objects.
Explicitly we are not transforming in the code but behind the scenes, it is needed. For instance, consider this code:
@router.get("/blog/{id}", response_model=ShowBlog)
def get_blog(id: int, db: Session = Depends(get_db)):
blog = retreive_blog(id=id, db=db)
return blog
Notice the response_model=ShowBlog
Normally, pydantic expects json as input. But in this case, we are sending a SQLAlchemy model object as a Json response. To parse this, we have to tell Pydantic that the input will be a orm object and the output will be JSON.
My priority is to help build a production-ready application using FastAPI
I prioritize quality over simplicity. Our challenging approach ensures you're prepared for real-world development.
© Copyright 2022-2025 Team FastAPITutorial. All rights reserved.