mirror of https://github.com/tiangolo/fastapi.git
Add read_with_orm_mode for dataclasses
This allows for a workaround for SQLAlchemy dataclasses after the changes introduced in 0.67 . It replicates the funcionality provided for Pydantic models (`read_with_orm_mode`) by allowing a special property to be set on dataclasses that want to opt in to the feature.
This commit is contained in:
parent
864643ef76
commit
bc8052a196
|
|
@ -99,6 +99,11 @@ def _prepare_response_content(
|
|||
for k, v in res.items()
|
||||
}
|
||||
elif dataclasses.is_dataclass(res):
|
||||
# Allow dataclasses to specify the same read_with_orm_mode property as
|
||||
# Pydantic models above
|
||||
read_with_orm_mode = getattr(res, "__read_with_orm_mode__", None)
|
||||
if read_with_orm_mode:
|
||||
return res
|
||||
return dataclasses.asdict(res)
|
||||
return res
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue