Debian Codegraph @

docs/examples/data_transfer_objects/factory/tutorial/dto_data.py

raw
1 from __future__ import annotations
2
3 from dataclasses import dataclass
4
5 from litestar import Litestar, post
6 from litestar.dto import DataclassDTO, DTOConfig, DTOData
7
8
9 @dataclass
10 class Person:
11 name: str
12 age: int
13 email: str
14 id: int
15
16
17 class ReadDTO(DataclassDTO[Person]):
18 config = DTOConfig(exclude={"email"})
19
20
21 class WriteDTO(DataclassDTO[Person]):
22 config = DTOConfig(exclude={"id"})
23
24
25 @post("/person", dto=WriteDTO, return_dto=ReadDTO, sync_to_thread=False)
26 def create_person(data: DTOData[Person]) -> Person:
27 # Logic for persisting the person goes here
28 return data.create_instance(id=1)
29
30
31 app = Litestar(route_handlers=[create_person])