First Check
Commit to Help
Example Code
class Team(SQLModel):
id: int = Field(default=None, primary_key=True)
heroes: List["Hero"] = Relationship(back_populates="team", sa_relationship_kwargs={"lazy": "selectin"})
class Hero(SQLModel):
name: str
team_id: Optional[int] = Field(default=None, foreign_key="team.id")
team: Optional[Team] = Relationship(back_populates="incidents")
app = FastAPI()
@app.get("/teams/", response_model=List[Team])
def get_teams() -> List[Team]:
with Session(engine) as session:
return session.exec(select(Team)).all()
desired_output = [
{
"id": 1,
"heroes": [
{"name": "Batman"},
{"name": "Superman"}
]
}
]
Description
When returning a list of Teams the Relationship fields are not covered in the output. I believe the response is using pydantic.main.BaseModel.json() to get the data (and there it will only cover non-Relationship fields).
It would be great if there was an option to also return the Relationship fields especially if {"lazy": "selectin"} was enabled for the relation.
It's an easy workaround to walk the objects and return the complete data as json in the endpoint, but since we already have the models that is not very satisfying (also because the response_model for openapi will not be easily achievable).
Did I miss an option to enable having the Relationship attributes in the response, or is that something that could be worked on?
I would also be happy to contribute but would need a pointer on where to start.
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.7
Python Version
3.10.4
Additional Context
No response
First Check
Commit to Help
Example Code
Description
When returning a list of Teams the
Relationshipfields are not covered in the output. I believe the response is usingpydantic.main.BaseModel.json()to get the data (and there it will only cover non-Relationshipfields).It would be great if there was an option to also return the
Relationshipfields especially if{"lazy": "selectin"}was enabled for the relation.It's an easy workaround to walk the objects and return the complete data as json in the endpoint, but since we already have the models that is not very satisfying (also because the response_model for openapi will not be easily achievable).
Did I miss an option to enable having the
Relationshipattributes in the response, or is that something that could be worked on?I would also be happy to contribute but would need a pointer on where to start.
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.7
Python Version
3.10.4
Additional Context
No response