-
I'm making a TV listing API and I've written an endpoint that should return a dict of dates mapping to a list of episodes for that day. I haven't defined the outer schema, but the model Episode has a EpisodeSchema defined which does a few things.
But if I do that, it explodes saying (on the EpisodeSchema constructor): Is there a way to build a ModelSchema instance from a model instance? Or am I going about this the wrong way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
try like this: @api.get("/schedule/")
def schedule(request):
# ... my logic to group episodes into dates ...
# then return
return {
d: [
EpisodeSchema.model_validate(ep)
for ep in grouped_episodes[d]
]
for d in dates
} or with response definition: @api.get("/schedule/", response=dict[str, list[EpisodeSchema])
def schedule(request):
# ... my logic to group episodes into dates ...
# then return
return {
d: list(grouped_episodes[d]) for d in dates
} |
Beta Was this translation helpful? Give feedback.
@oliwarner
try like this:
or with response definition: