We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
See here:
https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/#models-with-relationships
My example code
class StrategyWithPortfolios(StrategyWithCategory): portfolios: list[Portfolio] = [] @router.get("/all") @cache(expire=60) async def read_strategies( session: Session = Depends(get_session), ) -> list[StrategyWithPortfolios]: """ get all Strategy """ stmt = ( session.query(Strategy) .options( joinedload(Strategy.portfolios).options(joinedload(Portfolio.investors)) ) .options(joinedload(Strategy.category).load_only(Category.name)) ) ret = stmt.all() ret = [StrategyWithPortfolios.model_validate(r) for r in ret] # very important!!!!! return ret
The returned result is sqlalchemy orm class Strategy but the data structure I want to return is StrategyWithPortfolios.
Strategy
StrategyWithPortfolios
The code works well without the line ret = [StrategyWithPortfolios.model_validate(r) for r in ret] and @cache(expire=60).
ret = [StrategyWithPortfolios.model_validate(r) for r in ret]
@cache(expire=60)
But if I add @cache(expire=60) it save fields only belong to Strategy not portfolios nor Category to redis.
portfolios
Category
Please serialize the returned result to given pydantic model before save it to backend.
The text was updated successfully, but these errors were encountered:
No branches or pull requests
See here:
https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/#models-with-relationships
My example code
The returned result is sqlalchemy orm class
Strategy
but the data structure I want to return isStrategyWithPortfolios
.The code works well without the line
ret = [StrategyWithPortfolios.model_validate(r) for r in ret]
and@cache(expire=60)
.But if I add
@cache(expire=60)
it save fields only belong toStrategy
notportfolios
norCategory
to redis.Please serialize the returned result to given pydantic model before save it to backend.
The text was updated successfully, but these errors were encountered: