mirror of
https://github.com/rjNemo/fastapi
synced 2026-06-12 05:26:45 +00:00
✨ Add support for read_with_orm_mode, to support SQLModel relationship attributes (#3757)
This commit is contained in:
parent
f7d7c6c865
commit
20d4834546
2 changed files with 60 additions and 0 deletions
|
|
@ -65,6 +65,13 @@ def _prepare_response_content(
|
||||||
exclude_none: bool = False,
|
exclude_none: bool = False,
|
||||||
) -> Any:
|
) -> Any:
|
||||||
if isinstance(res, BaseModel):
|
if isinstance(res, BaseModel):
|
||||||
|
read_with_orm_mode = getattr(res.__config__, "read_with_orm_mode", None)
|
||||||
|
if read_with_orm_mode:
|
||||||
|
# Let from_orm extract the data from this model instead of converting
|
||||||
|
# it now to a dict.
|
||||||
|
# Otherwise there's no way to extract lazy data that requires attribute
|
||||||
|
# access instead of dict iteration, e.g. lazy relationships.
|
||||||
|
return res
|
||||||
return res.dict(
|
return res.dict(
|
||||||
by_alias=True,
|
by_alias=True,
|
||||||
exclude_unset=exclude_unset,
|
exclude_unset=exclude_unset,
|
||||||
|
|
|
||||||
53
tests/test_read_with_orm_mode.py
Normal file
53
tests/test_read_with_orm_mode.py
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class PersonBase(BaseModel):
|
||||||
|
name: str
|
||||||
|
lastname: str
|
||||||
|
|
||||||
|
|
||||||
|
class Person(PersonBase):
|
||||||
|
@property
|
||||||
|
def full_name(self) -> str:
|
||||||
|
return f"{self.name} {self.lastname}"
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
read_with_orm_mode = True
|
||||||
|
|
||||||
|
|
||||||
|
class PersonCreate(PersonBase):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PersonRead(PersonBase):
|
||||||
|
full_name: str
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
orm_mode = True
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/people/", response_model=PersonRead)
|
||||||
|
def create_person(person: PersonCreate) -> Any:
|
||||||
|
db_person = Person.from_orm(person)
|
||||||
|
return db_person
|
||||||
|
|
||||||
|
|
||||||
|
client = TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
|
def test_read_with_orm_mode() -> None:
|
||||||
|
person_data = {"name": "Dive", "lastname": "Wilson"}
|
||||||
|
response = client.post("/people/", json=person_data)
|
||||||
|
data = response.json()
|
||||||
|
assert response.status_code == 200, response.text
|
||||||
|
assert data["name"] == person_data["name"]
|
||||||
|
assert data["lastname"] == person_data["lastname"]
|
||||||
|
assert data["full_name"] == person_data["name"] + " " + person_data["lastname"]
|
||||||
Loading…
Reference in a new issue