There are requirements for "create many" or "update many" endpoints that may involve processing a list of objects. Currently, the DTOs have a limitation in that they cannot receive data as a list of DTO instances. Would it be considered to have this feature included?
from dataclasses import dataclass
from datetime import date
from typing import Annotated
from litestar import Litestar, post
from litestar.dto import DataclassDTO, DTOConfig, DTOData
@dataclass
class Author:
id: int
name: str
dob: date
AuthorWrite = DataclassDTO[Annotated[Author, DTOConfig({"id"})]]
@post("/", dto=AuthorWrite, return_dto=None)
async def create_many(data: DTOData[list[Author]]) -> list[Author]:
# expected: list of dicts
# current: empty {}
print(data.as_builtins())
# expected: being able to iterate over `data`
# create instances of the DTO validated data.
# current: TypeError: 'DTOData' object is not iterable
return [i.create_instance(id=1) for i in data]
app = Litestar([create_many])
Without this feature being natively supported, one has to resort to writing separate classes for receiving the data. This, to me, doesn't fully utilize the potential of DTOs.
@dataclass
class AuthorCreate:
name: str
dob: date
If this feature is under consideration, I will leave these points for the maintainers to consider:
DTOData
instance be iterable if a list of objects is passed?data.create_instances()
?data.create_instances
is chosen? data.create_instance(id=1)
vs data.create_instances([{'id': 1}])
?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