Skip to content

Creating grouped SQLAlchemyDTO return object #3586

Answered by thomastu
ste-pool asked this question in Q&A
Discussion options

You must be logged in to vote

I think you're looking for the pattern documented here: https://docs.litestar.dev/2/usage/dto/1-abstract-dto.html#wrapping-return-data

from typing import Generic

class Foo(Base):
    __tablename__ = "foo"
    id: Mapped[String] = mapped_column(String, primary_key=True)

FooDTO = SQLAlchemyDTO[Foo]

T = TypeVar("T")

@dataclass
class Bar(Generic[T]):

    foos: list[T]
    blah: str

@get("/get_bar", dto=FooDTO)
async def get_bar(db_session: AsyncSession) -> Bar[Foo]:
    foos = await db_session.scalars(select(Foo))
    return Bar(foos=foos.all(), blah="hello")

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@ste-pool
Comment options

@ste-pool
Comment options

@thomastu
Comment options

@ste-pool
Comment options

Answer selected by ste-pool
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants