作者:麥克煎蛋 出處:https://www.cnblogs.com/mazhiyong/ 轉載請保留這段聲明,謝謝!
一、Pydantic模型的附加信息
與前面講過的Query、Path、Body類似,我們也可以為Pydantic模型添加附加信息,基於模塊Field。
1、導入Field模塊
from pydantic import BaseModel, Field
2、聲明模型屬性
class Item(BaseModel): name: str description: str = Field(None, title="The description of the item", max_length=300) price: float = Field(..., gt=0, description="The price must be greater than zero") tax: float = None
Field模塊的參數與Query、Path、Body等相同。
完整示例:
from fastapi import Body, FastAPI from pydantic import BaseModel, Field app = FastAPI() class Item(BaseModel): name: str description: str = Field(None, title="The description of the item", max_length=300) price: float = Field(..., gt=0, description="The price must be greater than zero") tax: float = None @app.put("/items/{item_id}") async def update_item(*, item_id: int, item: Item = Body(..., embed=True)): results = {"item_id": item_id, "item": item} return results
二、Pydantic嵌套模型
任意嵌套深度的Pydantic模型,我們都可以進行定義、校驗、自動文檔化和靈活使用。
1、模型的屬性可以是數據集合類型
比如list,dict,tuple,set等等。
class Item(BaseModel): name: str description: str = None price: float tax: float = None tags: list = []
from typing import List class Item(BaseModel): name: str description: str = None price: float tax: float = None tags: List[str] = []
from typing import Set class Item(BaseModel): name: str description: str = None price: float tax: float = None tags: Set[str] = set()
我們也可以直接定義一個字典類型的body,通常指定了鍵和值的數據類型。
from typing import Dict from fastapi import FastAPI app = FastAPI() @app.post("/index-weights/") async def create_index_weights(weights: Dict[int, float]): return weights
2、嵌套模型
每一個Pydantic模型的屬性都有一個類型,這個類型也可以是另一個Pydantic模型。
from typing import Set from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Image(BaseModel): url: str name: str class Item(BaseModel): name: str description: str = None price: float tax: float = None tags: Set[str] = [] image: Image = None @app.put("/items/{item_id}") async def update_item(*, item_id: int, item: Item): results = {"item_id": item_id, "item": item} return results
FastAPI期望的Request Body內容格式如下:
{ "name": "Foo", "description": "The pretender", "price": 42.0, "tax": 3.2, "tags": ["rock", "metal", "bar"], "image": { "url": "http://example.com/baz.jpg", "name": "The Foo live" } }
我們也可以把Pydantic模型作為list、set等集合類型的元素類型。
from typing import List, Set from fastapi import FastAPI from pydantic import BaseModel, HttpUrl app = FastAPI() class Image(BaseModel): url: HttpUrl name: str class Item(BaseModel): name: str description: str = None price: float tax: float = None tags: Set[str] = [] images: List[Image] = None @app.put("/items/{item_id}") async def update_item(*, item_id: int, item: Item): results = {"item_id": item_id, "item": item} return results
這里FastAPI期望的Request Body內容格式如下:
{ "name": "Foo", "description": "The pretender", "price": 42.0, "tax": 3.2, "tags": [ "rock", "metal", "bar" ], "images": [ { "url": "http://example.com/baz.jpg", "name": "The Foo live" }, { "url": "http://example.com/dave.jpg", "name": "The Baz" } ] }
3、深層嵌套模型
我們可以定義任意深度的嵌套模型,如下示例:
from typing import List, Set from fastapi import FastAPI from pydantic import BaseModel, HttpUrl app = FastAPI() class Image(BaseModel): url: HttpUrl name: str class Item(BaseModel): name: str description: str = None price: float tax: float = None tags: Set[str] = [] images: List[Image] = None class Offer(BaseModel): name: str description: str = None price: float items: List[Item] @app.post("/offers/") async def create_offer(*, offer: Offer): return offer