[FastAPI-22]响应模型-response_model

import typing

from fastapi import FastAPI, Response
from fastapi.responses import JSONResponse
from pydantic import BaseModel

app = FastAPI()

'''
响应模型
swagger Example显示
'''


class BaseUser(BaseModel):
    username: str
    email : str

class UserIn(BaseUser):
    password : str
    re_password : str

class UserOut(BaseUser):
    pass

@app.post("/register",response_model=UserOut)
def register(user : UserIn):
    # 注册用户的操作
    return user

@app.post("/demo",response_model=typing.Dict[str,str])
# 字段类型转换
def dmeo():
    return {
        "name":"Tom",
        "age":18
    }
posted @ 2023-03-25 12:40  LeoShi2020  阅读(83)  评论(0编辑  收藏  举报