额外数据类型
到目前为止,您一直在使用常见的数据类型,如:
intfloatstrbool
但是您也可以使用更复杂的数据类型。
您仍然会拥有现在已经看到的相同的特性:
- 很棒的编辑器支持。
- 传入请求的数据转换。
- 响应数据转换。
- 数据验证。
- 自动补全和文档。
其他数据类型
下面是一些你可以使用的其他数据类型:
UUID:- 一种标准的 “通用唯一标识符” ,在许多数据库和系统中用作ID。
- 在请求和响应中将以
str表示。
datetime.datetime:- 一个 Python
datetime.datetime. - 在请求和响应中将表示为 ISO 8601 格式的
str,比如:2008-09-15T15:53:00+05:00.
- 一个 Python
datetime.date:- Python
datetime.date. - 在请求和响应中将表示为 ISO 8601 格式的
str,比如:2008-09-15.
- Python
datetime.time:- 一个 Python
datetime.time. - 在请求和响应中将表示为 ISO 8601 格式的
str,比如:14:23:55.003.
- 一个 Python
datetime.timedelta:- 一个 Python
datetime.timedelta. - 在请求和响应中将表示为
float代表总秒数。 - Pydantic 也允许将其表示为 “ISO 8601 时间差异编码”, 查看文档了解更多信息。
- 一个 Python
frozenset:- 在请求和响应中,作为
set对待:- 在请求中,列表将被读取,消除重复,并将其转换为一个
set。 - 在响应中
set将被转换为list。 - 产生的模式将指定那些
set的值是唯一的 (使用 JSON 模式的uniqueItems)。
- 在请求中,列表将被读取,消除重复,并将其转换为一个
- 在请求和响应中,作为
bytes:- 标准的 Python
bytes。 - 在请求和响应中被当作
str处理。 - 生成的模式将指定这个
str是binary“格式”。
- 标准的 Python
Decimal:- 标准的 Python
Decimal。 - 在请求和响应中被当做
float一样处理。
- 标准的 Python
- 您可以在这里检查所有有效的pydantic数据类型: Pydantic data types.
例子
下面是一个路径操作的示例,其中的参数使用了上面的一些类型。
Python 3.10+Python 3.9+Python 3.8+Python 3.10+ non-AnnotatedPython 3.8+ non-Annotated
from datetime import datetime, time, timedelta from typing import Annotatedfrom uuid import UUIDfrom fastapi import Body, FastAPIapp = FastAPI()@app.put("/items/{item_id}")async def read_items(item_id: UUID, start_datetime: Annotated[datetime, Body()], end_datetime: Annotated[datetime, Body()], process_after: Annotated[timedelta, Body()], repeat_at: Annotated[time | None, Body()] = None, ):start_process = start_datetime + process_afterduration = end_datetime - start_processreturn {"item_id": item_id,"start_datetime": start_datetime,"end_datetime": end_datetime,"process_after": process_after,"repeat_at": repeat_at,"start_process": start_process,"duration": duration,}`
from datetime import datetime, time, timedelta from typing import Annotated, Unionfrom uuid import UUIDfrom fastapi import Body, FastAPIapp = FastAPI()@app.put("/items/{item_id}")async def read_items(item_id: UUID, start_datetime: Annotated[datetime, Body()], end_datetime: Annotated[datetime, Body()], process_after: Annotated[timedelta, Body()], repeat_at: Annotated[Union[time, None], Body()] = None, ):start_process = start_datetime + process_afterduration = end_datetime - start_processreturn {"item_id": item_id,"start_datetime": start_datetime,"end_datetime": end_datetime,"process_after": process_after,"repeat_at": repeat_at,"start_process": start_process,"duration": duration,}`
from datetime import datetime, time, timedelta from typing import Unionfrom uuid import UUIDfrom fastapi import Body, FastAPIfrom typing_extensions import Annotatedapp = FastAPI()@app.put("/items/{item_id}")async def read_items(item_id: UUID, start_datetime: Annotated[datetime, Body()], end_datetime: Annotated[datetime, Body()], process_after: Annotated[timedelta, Body()], repeat_at: Annotated[Union[time, None], Body()] = None, ):start_process = start_datetime + process_afterduration = end_datetime - start_processreturn {"item_id": item_id,"start_datetime": start_datetime,"end_datetime": end_datetime,"process_after": process_after,"repeat_at": repeat_at,"start_process": start_process,"duration": duration,}`
Tip
尽可能选择使用 Annotated 的版本。
from datetime import datetime, time, timedelta from uuid import UUIDfrom fastapi import Body, FastAPIapp = FastAPI()@app.put("/items/{item_id}")async def read_items(item_id: UUID, start_datetime: datetime = Body(), end_datetime: datetime = Body(), process_after: timedelta = Body(), repeat_at: time | None = Body(default=None), ):start_process = start_datetime + process_afterduration = end_datetime - start_processreturn {"item_id": item_id,"start_datetime": start_datetime,"end_datetime": end_datetime,"process_after": process_after,"repeat_at": repeat_at,"start_process": start_process,"duration": duration,}`
Tip
尽可能选择使用 Annotated 的版本。
from datetime import datetime, time, timedelta from typing import Union from uuid import UUIDfrom fastapi import Body, FastAPIapp = FastAPI()@app.put("/items/{item_id}")async def read_items(item_id: UUID, start_datetime: datetime = Body(), end_datetime: datetime = Body(), process_after: timedelta = Body(), repeat_at: Union[time, None] = Body(default=None), ):start_process = start_datetime + process_afterduration = end_datetime - start_processreturn {"item_id": item_id,"start_datetime": start_datetime,"end_datetime": end_datetime,"process_after": process_after,"repeat_at": repeat_at,"start_process": start_process,"duration": duration,}`
注意,函数内的参数有原生的数据类型,你可以,例如,执行正常的日期操作,如:
Python 3.10+Python 3.9+Python 3.8+Python 3.10+ non-AnnotatedPython 3.8+ non-Annotated
from datetime import datetime, time, timedeltafrom typing import Annotatedfrom uuid import UUIDfrom fastapi import Body, FastAPIapp = FastAPI()@app.put("/items/{item_id}")async def read_items(item_id: UUID,start_datetime: Annotated[datetime, Body()],end_datetime: Annotated[datetime, Body()],process_after: Annotated[timedelta, Body()],repeat_at: Annotated[time | None, Body()] = None,):start_process = start_datetime + process_after duration = end_datetime - start_process return {"item_id": item_id,"start_datetime": start_datetime,"end_datetime": end_datetime,"process_after": process_after,"repeat_at": repeat_at,"start_process": start_process,"duration": duration,}`
