网站文化制度建设,phpcms v9企业网站模板:蓝色电子科技公司网站模板,网页设计规范要求,什么网站做软文文章目录1. 混合使用 Path、Query 和请求体参数2. 多个请求体参数3. 请求体中的单一值4. 多个请求体参数和查询参数5. 嵌入单个请求体参数6. 字段7. 嵌套模型7.1 List 字段7.2 子模型作为类型8. 特殊类型校验9. 带有一组子模型的属性10. 任意 dict 构成的请求体learn from http…
文章目录1. 混合使用 Path、Query 和请求体参数2. 多个请求体参数3. 请求体中的单一值4. 多个请求体参数和查询参数5. 嵌入单个请求体参数6. 字段7. 嵌套模型7.1 List 字段7.2 子模型作为类型8. 特殊类型校验9. 带有一组子模型的属性10. 任意 dict 构成的请求体learn from https://fastapi.tiangolo.com/zh/tutorial/body-multiple-params/
1. 混合使用 Path、Query 和请求体参数
from fastapi import FastAPI, Path
from typing import Optional
from pydantic import BaseModel
app FastAPI()class Item1(BaseModel):name: strprice: floatdescription: Optional[str] Nonetax: Optional[float]app.put(/items/{item_id})
async def update_item(*,item_id: int Path(..., titleid of item to get, ge0, le1000),q: Optional[str] None,item: Optional[Item1] None,
):res {item_id: item_id}if q:res.update({q: q})if item:res.update({item: item})return res2. 多个请求体参数
from pydantic import BaseModelclass Item(BaseModel):name: strprice: floatdescription: Optional[str] Nonetax: Optional[float]
class User(BaseModel):username: strfull_name: Optional[str] Noneapp.put(/items/{item_id})
async def update_item(item_id: int, item: Item, user: User):res {item_id : item_id, item : item, user: user}return res使用 参数名称 最为 key 的 字典传入
3. 请求体中的单一值
传参时varname : type Body(...)如果不这么写会被作为查询参数 ?varnamexxx
from fastapi import Bodyclass Item(BaseModel):name: strprice: floatdescription: Optional[str] Nonetax: Optional[float]
class User(BaseModel):username: strfull_name: Optional[str] Noneapp.put(/items/{item_id})
async def update_item(item_id: int, item: Item, user: User, importance : int Body(...)):res {item_id : item_id, item : item, user: user}return res现在可以写在请求体内
4. 多个请求体参数和查询参数
由于默认情况下单一值被解释为查询参数因此你不必显式地添加 Query你可以仅执行操作q: str None
5. 嵌入单个请求体参数
如果你只有一个请求体参数
app.put(/items/{item_id})
async def update_item(item_id: int, item: Item):res {item_id : item_id, item : item}return res你的请求体需要写成如下形式 如果你想写成 带 key 的 json 形式添加一个传入参数 embeditem: Item Body(..., embedTrue)
6. 字段
可以使用 Pydantic 的 Field 在 Pydantic 模型内部声明校验和元数据
from fastapi import FastAPI, Path, Body
from typing import Optional
from pydantic import BaseModel, Field
app FastAPI()class Item(BaseModel):name: strprice: float Field(..., gt0, descriptionprice must be greater than 0)description: Optional[str] Field(None, titledescription of item, max_length30)tax: Optional[float] Noneapp.put(/items/{item_id})
async def update_item(item_id: int, item: Item Body(..., embedTrue)):res {item_id : item_id, item : item}return resField 的工作方式和 Query、Path 和 Body 相同包括它们的参数等等也完全相同
注意from pydantic import Field
7. 嵌套模型
7.1 List 字段
将一个属性定义为拥有子元素的类型如 list
class Item(BaseModel):name: strprice: float Field(..., gt0, descriptionprice must be greater than 0)description: Optional[str] Field(None, titledescription of item, max_length30)tax: Optional[float] Nonetags: list [] # 没有声明元素类型具有子类型的 Listfrom typing import List
tags: List[str] []7.2 子模型作为类型
from fastapi import FastAPI, Path, Body
from typing import Optional, List, Set
from pydantic import BaseModel, Field
app FastAPI()class Image(BaseModel):url:strname: strclass Item(BaseModel):name: strprice: float Field(..., gt0, descriptionprice must be greater than 0)description: Optional[str] Field(None, titledescription of item, max_length30)tax: Optional[float] Nonetags: Set[str] []image: Optional[Image] Noneapp.put(/items/{item_id})
async def update_item(item_id: int, item: Item Body(..., embedTrue)):res {item_id : item_id, item : item}return res8. 特殊类型校验
HttpUrl检查是不是有效的 URL
from pydantic import BaseModel, Field, HttpUrl
app FastAPI()class Image(BaseModel):url: HttpUrlname: str则上面的输入应改的地方 url:http://www.michael.com,否则不是有效的 URL
9. 带有一组子模型的属性
更改为 image: Optional[List[Image]] None 输入需要改为
app.post(/images/multiple/)
async def create_multiple_images(images: List[Image]):return images10. 任意 dict 构成的请求体
from typing import Optional, List, Set, Dict
app.post(/index-weights/)
async def create_index_weights(weights: Dict[int, float]): # key 为 int value 为浮点return weights请记住 JSON 仅支持将 str 作为键。 但是 Pydantic 具有自动转换数据的功能。