Skip to content
Discussion options

You must be logged in to vote

For now, I opted for the following solution:

TeamResponse = Union[TeamRead, TeamReadWithHeroes]

@app.get("/teams/{team_id}", response_model=TeamResponse)
def read_team(*, team_id: int, include_heroes: bool = False, session: Session = Depends(get_session)):
    model_class = TeamRead
    if include_heroes:
        model_class = TeamReadWithHeroes
    team = session.get(Team, team_id)
    if not team:
        raise HTTPException(status_code=404, detail="Team not found")
    return model_class.model_validate(team)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
1 participant