I’m trying to serialize to json or dict list of models of SQLAlchemy to then assert it with response.json():
from datetime import date
from typing import Annotated, Generic, TypeVar
from pydantic import BaseModel, Field
ResultItemSchema = TypeVar("ResultItemSchema", bound=BaseModel)
class TtzSchema(BaseModel):
id: int
name: str
start_date: date
class PaginatedResponseSchema(BaseModel, Generic[ResultItemSchema]):
count: int
results: list[ResultItemSchema]
class Ttz(Base):
__tablename__ = "ttz"
id: int
name: str
start_date: date
cnt = 3
results = [Ttz(...) for i in range(cnt)]
PaginatedResponseSchema[TtzSchema].model_validate({"count": cnt, "results": results}) # does not work. how to serialize to json?
But this does not work.