FastAPI 学习之路(十四)响应模型

简介: FastAPI 学习之路(十四)响应模型

系列文章:

  FastAPI 学习之路(一)fastapi--高性能web开发框架

  FastAPI 学习之路(二)

  FastAPI 学习之路(三)

  FastAPI 学习之路(四)

  FastAPI 学习之路(五)

     FastAPI 学习之路(六)查询参数,字符串的校验

  FastAPI 学习之路(七)字符串的校验

   FastAPI 学习之路(八)路径参数和数值的校验

  FastAPI 学习之路(九)请求体有多个参数如何处理?

  FastAPI 学习之路(十)请求体的字段

     FastAPI 学习之路(十一)请求体 - 嵌套模型

     FastAPI 学习之路(十二)接口几个额外信息和额外数据类型


我们可以在我们不同的请求路径的返回参数使用响应模型。我们看一个简单的demo。


from typing import List, Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class One(BaseModel):
    name: str
    description: Optional[str] = None
    price: float
    tax: Optional[float] = None
@app.post("/items/", response_model=One)
def create_item(item: One):
    return item


我们可以看下,接口的正常返回


image.png


esponse_model是「装饰器」方法(get,post 等)的一个参数。不像之前的所有参数和请求体,它不属于路径操作函数。    

   

它接收的类型与你将为 Pydantic 模型属性所声明的类型相同,因此它可以是一个 Pydantic 模型,但也可以是一个由 Pydantic 模型组成的 list,例如 List[Item]。

FastAPI 将使用此 response_model 来:


  • 将输出数据转换为其声明的类型。


  • 校验数据。


  • 在 OpenAPI 的路径操作中为响应添加一个 JSON Schema。


  • 并在自动生成文档系统中使用。


但最重要的是:


  • 会将输出数据限制在该模型定义内。


       我们下面做一个演示,我们正常的都应该知道,我们去创建用户的时候呢,我们的密码是明文的,我们要返回的用户信息中,不能携带我们的密码,我们应该如何处理呢,其实很简单


from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class UserIn(BaseModel):
    username: str
    password: str
    email: str
    full_name: Optional[str] = None
class Userout(BaseModel):
    username: str
    email: str
    full_name: Optional[str] = None
@app.post("/user/", response_model=Userout)
def create_user(user: UserIn):
    return user


我们看下接口的实际返回


image.png


我们看下接口的文档的展示


image.png


我们在接口的请求中,如果不传递,我不想要返回带默认值的


image.png


我们看下代码如何实现的


from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class UserIn(BaseModel):
    username: str
    password: str
    email: str
    full_name: Optional[str] = None
class Userout(BaseModel):
    username: str
    email: str
    full_name: Optional[str] = None
@app.post("/user/", response_model=Userout,response_model_exclude_unset=True)
def create_user(user: UserIn):
    return user


其实就是response_model_exclude_unset来处理,我们看下实际的效果


image.png


那么这个时候,我们传递了呢。


image.png


可以看到,我们传递了参数就可以正常的展示,不传递参数的,我们不返回默认的值。


相关文章
|
4天前
|
JSON API 持续交付
逐步指南:使用FastAPI部署YOLO模型的步骤
逐步指南:使用FastAPI部署YOLO模型的步骤
|
数据库
FastAPI(53)- Response Headers 响应设置 Headers
FastAPI(53)- Response Headers 响应设置 Headers
472 0
FastAPI(53)- Response Headers 响应设置 Headers
|
7月前
|
JSON Kubernetes API
使用FastAPI部署Ultralytics YOLOv5模型
YOLO是You Only Look Once(你只看一次)的缩写,它具有识别图像中的物体的非凡能力,在日常应用中会经常被使用。所以在本文中,我们将介绍如何使用FastAPI的集成YOLOv5,这样我们可以将YOLOv5做为API对外提供服务。
168 5
|
9月前
|
JSON NoSQL API
全面拥抱 FastApi — 响应模型
全面拥抱 FastApi — 响应模型
|
机器学习/深度学习 Python
【Python】fastapi框架之Web部署机器学习模型
【Python】fastapi框架之Web部署机器学习模型
|
XML 数据格式
FastAPI(47)- 通过 Response 自定义响应的类型
FastAPI(47)- 通过 Response 自定义响应的类型
258 0
FastAPI(47)- 通过 Response 自定义响应的类型
|
JavaScript API 网络安全
FastAPI(52)- Response Cookies 响应设置 Cookies
FastAPI(52)- Response Cookies 响应设置 Cookies
331 0
FastAPI(52)- Response Cookies 响应设置 Cookies
|
存储
FastAPI(51)- 自定义响应之 StreamingResponse、FileResponse
FastAPI(51)- 自定义响应之 StreamingResponse、FileResponse
829 0
FastAPI(51)- 自定义响应之 StreamingResponse、FileResponse
FastAPI(50)- 自定义响应之 RedirectResponse
FastAPI(50)- 自定义响应之 RedirectResponse
288 0
FastAPI(50)- 自定义响应之 RedirectResponse
|
JSON API 数据格式
FastAPI(49)- 自定义响应之 ORJSONResponse、UJSONResponse
FastAPI(49)- 自定义响应之 ORJSONResponse、UJSONResponse
368 0
FastAPI(49)- 自定义响应之 ORJSONResponse、UJSONResponse