1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
from __future__ import annotations
from typing import TYPE_CHECKING
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from litestar import Litestar, post
from litestar.dto import dto_field
from litestar.plugins.sqlalchemy import SQLAlchemySerializationPlugin
if TYPE_CHECKING:
from typing import List
class Base(DeclarativeBase): ...
class TodoItem(Base):
__tablename__ = "todo_item"
title: Mapped[str] = mapped_column(primary_key=True)
done: Mapped[bool]
super_secret_value: Mapped[str] = mapped_column(info=dto_field("private"))
@post("/")
async def add_item(data: TodoItem) -> List[TodoItem]:
data.super_secret_value = "This is a secret"
return [data]
app = Litestar(route_handlers=[add_item], plugins=[SQLAlchemySerializationPlugin()])
|