Skip to content

How to initialise ModelSchema instances explicitly? #880

Answered by vitalik
oliwarner asked this question in Q&A
Discussion options

You must be logged in to vote

@oliwarner

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
    }

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@oliwarner
Comment options

Answer selected by oliwarner
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