fastapi设置响应示例

class Test(BaseModel):
    name: str
    description: int
    created_at: str
    updated_at: str
    data: dict

    @validator("data", pre=True)
    def parse_data(cls, value):
        return json.loads(value) if value else {}

    @validator("created_at", pre=True)
    def parse_created_at(cls, value):
        return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(value)))

    @validator("updated_at", pre=True)
    def parse_updated_at(cls, value):
        return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int(value)))

    class Config:
        orm_mode = True
        schema_extra = {
            "example": {
                "name": "Foo",
                "description": "A very nice Item",
                "created_at": "",
                "updated_at": "",
                "data": {}
            }
        }
posted @ 2023-09-14 14:16  bitterteaer  阅读(26)  评论(0编辑  收藏  举报