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:
Gabriel Soicher 2021-10-27 09:18:17 +02:00
parent 864643ef76
commit bc8052a196
1 changed files with 5 additions and 0 deletions

View File

@ -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