As per the documentation and answer in Discord, DTO can work with generic types such as dict and tuple. Expected result of the example below is
{"name":"John","pets":[{"nickname":"Dog"},{"color":"White"}]}
Currently the result is
{"name":"John","pets":[{"id":2,"nickname":"Dog"},{"id":3,"color":"White"}]}
No response
from dataclasses import dataclass
from litestar import Litestar, get
from litestar.dto import DTOConfig, DataclassDTO
@dataclass
class Dog:
id: int
nickname: str
@dataclass
class Cat:
id: int
color: str
@dataclass
class Person:
id: int
name: str
pets: tuple[Dog, Cat]
class ReadDTO(DataclassDTO[Person]):
config = DTOConfig(
include={"name", "pets.0.name", "pets.1.color"}
)
@get("/", return_dto=ReadDTO)
async def index() -> Person:
person = Person(id=1, name="John", pets=(
Dog(id=2, nickname="Dog"),
Cat(id=3, color="White"),
))
return person
app = Litestar(route_handlers=[index], debug=True)
No response
"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"
No response
litestar[standard,pydantic]>=2.8.0
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too