FastAPI 教程翻译 - 用户指南 12 - 额外的数据类型
FastAPI Tutorial - User Guide - Extra data types
Up to now, you have been using common data types, like:
到目前为止,您一直在使用常见的数据类型,例如:
int
float
str
bool
But you can also use more complex data types.
但是您也可以使用更复杂的数据类型。
And you will still have the same features as seen up to now:
当然您仍将拥有相同的功能:
-
Great editor support.
强大的编辑器支持。
-
Data conversion from incoming requests.
来自传入请求的数据转换。
-
Data conversion for response data.
响应数据的数据转换。
-
Data validation.
数据验证。
-
Automatic annotation and documentation.
自动注释和文档。
Other data types
其他数据类型
Here are some of the additional data types you can use:
以下是一些您可以使用的其他数据类型:
-
UUID:
- A standard “Universally Unique Identifier”, common as an ID in many databases and systems.
标准的『通用唯一标识符』,在许多数据库和系统中通常作为 ID 使用。
-
In requests and responses will be represented as a
str
.在请求和响应中将以
str
表示。
-
datetime.datetime:
- A Python
datetime.datetime
.
一个 Python 类型
datetime.datetime
。-
In requests and responses will be represented as a
str
in ISO 8601 format, like:2008-09-15T15:53:00+05:00
.在请求和响应中,将以 ISO 8601 格式表示为
str
,例如:2008-09-15T15:53:00+05:00
。
- A Python
-
datetime.date:
- Python
datetime.date
.
一个 Python 类型
datetime.date
。-
In requests and responses will be represented as a
str
in ISO 8601 format, like:2008-09-15
.在请求和响应中,将以 ISO 8601 格式表示为
str
,例如:2008-09-15
。
- Python
-
datetime.time:
- A Python
datetime.time
.
一个 Python 类型
datetime.time
。-
In requests and responses will be represented as a
str
in ISO 8601 format, like:14:23:55.003
.在请求和响应中,将以 ISO 8601 格式表示为
str
,例如:14:23:55.003
。
- A Python
-
datetime.timedelta:
-
A Python
datetime.timedelta
.一个 Python 类型
datetime.timedelta
-
In requests and responses will be represented as a
float
of total seconds.在请求和响应中,将以
float
格式表示总秒数。 -
Pydantic also allows representing it as a “ISO 8601 time diff encoding”, see the docs for more info.
Pydantic 还允许将其表示为『ISO 8601时间差异编码』,请参阅文档以了解更多信息。
-
-
frozenset:
-
In requests and responses, treated the same as a
set
:在请求和响应中,将其视为
set
: -
In requests, a list will be read, eliminating duplicates and converting it to a
set
.在请求中,将读取列表,消除重复,并将其转换为
set
。-
In responses, the
set
will be converted to alist
.在响应中,
set
将被转换为list
。 -
The generated schema will specify that the
set
values are unique (using JSON Schema’suniqueItems
).生成的模式将指定唯一的
set
值(使用 JSON 模式的uniqueItems
)。
-
-
-
bytes:
-
Standard Python
bytes
.标准的 Python 类型
bytes
。 -
In requests and responses will be treated as
str
.在请求和响应中将被视为
str
。 -
The generated schema will specify that it’s a
str
withbinary
“format”.生成的模式将指定它是带有
binary
『格式』的str
。
-
-
Decimal:
-
Standard Python
Decimal
.标准的 Python 类型
Decimal
。 -
In requests and responses, handled the same as a
float
.在请求和响应中,处理方式与
float
相同。
-
Example
示例
Here’s an example path operation with parameters using some of the above types.
这是一个使用上述某些类型的带有参数的路径操作示例。
from datetime import datetime, time, timedelta
from uuid import UUID
from fastapi import Body, FastAPI
app = FastAPI()
@app.put("/items/{item_id}")
async def read_items(
item_id: UUID,
start_datetime: datetime = Body(None),
end_datetime: datetime = Body(None),
repeat_at: time = Body(None),
process_after: timedelta = 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,
"repeat_at": repeat_at,
"process_after": process_after,
"start_process": start_process,
"duration": duration,
}
Note that the parameters inside the function have their natural data type, and you can, for example, perform normal date manipulations, like:
请注意,函数内部的参数具有其自然数据类型,如,您可以执行常规的日期操作,例如:
from datetime import datetime, time, timedelta
from uuid import UUID
from fastapi import Body, FastAPI
app = FastAPI()
@app.put("/items/{item_id}")
async def read_items(
item_id: UUID,
start_datetime: datetime = Body(None),
end_datetime: datetime = Body(None),
repeat_at: time = Body(None),
process_after: timedelta = 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,
"repeat_at": repeat_at,
"process_after": process_after,
"start_process": start_process,
"duration": duration,
}