额外数据类型

到目前为止,您一直在使用常见的数据类型,如:

  • int
  • float
  • str
  • bool

但是您也可以使用更复杂的数据类型。

您仍然会拥有现在已经看到的相同的特性:

  • 很棒的编辑器支持。
  • 传入请求的数据转换。
  • 响应数据转换。
  • 数据验证。
  • 自动补全和文档。

其他数据类型

下面是一些你可以使用的其他数据类型:

  • UUID:
    • 一种标准的 “通用唯一标识符” ,在许多数据库和系统中用作ID。
    • 在请求和响应中将以 str 表示。
  • datetime.datetime:
    • 一个 Python datetime.datetime.
    • 在请求和响应中将表示为 ISO 8601 格式的 str ,比如: 2008-09-15T15:53:00+05:00.
  • datetime.date:
    • Python datetime.date.
    • 在请求和响应中将表示为 ISO 8601 格式的 str ,比如: 2008-09-15.
  • datetime.time:
    • 一个 Python datetime.time.
    • 在请求和响应中将表示为 ISO 8601 格式的 str ,比如: 14:23:55.003.
  • datetime.timedelta:
    • 一个 Python datetime.timedelta.
    • 在请求和响应中将表示为 float 代表总秒数。
    • Pydantic 也允许将其表示为 “ISO 8601 时间差异编码”, 查看文档了解更多信息
  • frozenset:
    • 在请求和响应中,作为 set 对待:
      • 在请求中,列表将被读取,消除重复,并将其转换为一个 set
      • 在响应中 set 将被转换为 list
      • 产生的模式将指定那些 set 的值是唯一的 (使用 JSON 模式的 uniqueItems)。
  • bytes:
    • 标准的 Python bytes
    • 在请求和相应中被当作 str 处理。
    • 生成的模式将指定这个 strbinary “格式”。
  • Decimal:
    • 标准的 Python Decimal
    • 在请求和相应中被当做 float 一样处理。
  • 您可以在这里检查所有有效的pydantic数据类型: Pydantic data types.

例子

下面是一个路径操作的示例,其中的参数使用了上面的一些类型。

Python 3.10+Python 3.9+Python 3.8+Python 3.10+ non-AnnotatedPython 3.8+ non-Annotated

  1. from datetime import datetime, time, timedelta
  2. from typing import Annotated
  3. from uuid import UUID
  4. from fastapi import Body, FastAPI
  5. app = FastAPI()
  6. @app.put("/items/{item_id}")
  7. async def read_items(
  8. item_id: UUID,
  9. start_datetime: Annotated[datetime | None, Body()] = None,
  10. end_datetime: Annotated[datetime | None, Body()] = None,
  11. repeat_at: Annotated[time | None, Body()] = None,
  12. process_after: Annotated[timedelta | None, Body()] = None,
  13. ):
  14. start_process = start_datetime + process_after
  15. duration = end_datetime - start_process
  16. return {
  17. "item_id": item_id,
  18. "start_datetime": start_datetime,
  19. "end_datetime": end_datetime,
  20. "repeat_at": repeat_at,
  21. "process_after": process_after,
  22. "start_process": start_process,
  23. "duration": duration,
  24. }
  1. from datetime import datetime, time, timedelta
  2. from typing import Annotated, Union
  3. from uuid import UUID
  4. from fastapi import Body, FastAPI
  5. app = FastAPI()
  6. @app.put("/items/{item_id}")
  7. async def read_items(
  8. item_id: UUID,
  9. start_datetime: Annotated[Union[datetime, None], Body()] = None,
  10. end_datetime: Annotated[Union[datetime, None], Body()] = None,
  11. repeat_at: Annotated[Union[time, None], Body()] = None,
  12. process_after: Annotated[Union[timedelta, None], Body()] = None,
  13. ):
  14. start_process = start_datetime + process_after
  15. duration = end_datetime - start_process
  16. return {
  17. "item_id": item_id,
  18. "start_datetime": start_datetime,
  19. "end_datetime": end_datetime,
  20. "repeat_at": repeat_at,
  21. "process_after": process_after,
  22. "start_process": start_process,
  23. "duration": duration,
  24. }
  1. from datetime import datetime, time, timedelta
  2. from typing import Union
  3. from uuid import UUID
  4. from fastapi import Body, FastAPI
  5. from typing_extensions import Annotated
  6. app = FastAPI()
  7. @app.put("/items/{item_id}")
  8. async def read_items(
  9. item_id: UUID,
  10. start_datetime: Annotated[Union[datetime, None], Body()] = None,
  11. end_datetime: Annotated[Union[datetime, None], Body()] = None,
  12. repeat_at: Annotated[Union[time, None], Body()] = None,
  13. process_after: Annotated[Union[timedelta, None], Body()] = None,
  14. ):
  15. start_process = start_datetime + process_after
  16. duration = end_datetime - start_process
  17. return {
  18. "item_id": item_id,
  19. "start_datetime": start_datetime,
  20. "end_datetime": end_datetime,
  21. "repeat_at": repeat_at,
  22. "process_after": process_after,
  23. "start_process": start_process,
  24. "duration": duration,
  25. }

Tip

尽可能选择使用 Annotated 的版本。

  1. from datetime import datetime, time, timedelta
  2. from uuid import UUID
  3. from fastapi import Body, FastAPI
  4. app = FastAPI()
  5. @app.put("/items/{item_id}")
  6. async def read_items(
  7. item_id: UUID,
  8. start_datetime: datetime | None = Body(default=None),
  9. end_datetime: datetime | None = Body(default=None),
  10. repeat_at: time | None = Body(default=None),
  11. process_after: timedelta | None = Body(default=None),
  12. ):
  13. start_process = start_datetime + process_after
  14. duration = end_datetime - start_process
  15. return {
  16. "item_id": item_id,
  17. "start_datetime": start_datetime,
  18. "end_datetime": end_datetime,
  19. "repeat_at": repeat_at,
  20. "process_after": process_after,
  21. "start_process": start_process,
  22. "duration": duration,
  23. }

Tip

尽可能选择使用 Annotated 的版本。

  1. from datetime import datetime, time, timedelta
  2. from typing import Union
  3. from uuid import UUID
  4. from fastapi import Body, FastAPI
  5. app = FastAPI()
  6. @app.put("/items/{item_id}")
  7. async def read_items(
  8. item_id: UUID,
  9. start_datetime: Union[datetime, None] = Body(default=None),
  10. end_datetime: Union[datetime, None] = Body(default=None),
  11. repeat_at: Union[time, None] = Body(default=None),
  12. process_after: Union[timedelta, None] = Body(default=None),
  13. ):
  14. start_process = start_datetime + process_after
  15. duration = end_datetime - start_process
  16. return {
  17. "item_id": item_id,
  18. "start_datetime": start_datetime,
  19. "end_datetime": end_datetime,
  20. "repeat_at": repeat_at,
  21. "process_after": process_after,
  22. "start_process": start_process,
  23. "duration": duration,
  24. }

注意,函数内的参数有原生的数据类型,你可以,例如,执行正常的日期操作,如:

Python 3.10+Python 3.9+Python 3.8+Python 3.10+ non-AnnotatedPython 3.8+ non-Annotated

  1. from datetime import datetime, time, timedelta
  2. from typing import Annotated
  3. from uuid import UUID
  4. from fastapi import Body, FastAPI
  5. app = FastAPI()
  6. @app.put("/items/{item_id}")
  7. async def read_items(
  8. item_id: UUID,
  9. start_datetime: Annotated[datetime | None, Body()] = None,
  10. end_datetime: Annotated[datetime | None, Body()] = None,
  11. repeat_at: Annotated[time | None, Body()] = None,
  12. process_after: Annotated[timedelta | None, Body()] = None,
  13. ):
  14. start_process = start_datetime + process_after
  15. duration = end_datetime - start_process
  16. return {
  17. "item_id": item_id,
  18. "start_datetime": start_datetime,
  19. "end_datetime": end_datetime,
  20. "repeat_at": repeat_at,
  21. "process_after": process_after,
  22. "start_process": start_process,
  23. "duration": duration,
  24. }
  1. from datetime import datetime, time, timedelta
  2. from typing import Annotated, Union
  3. from uuid import UUID
  4. from fastapi import Body, FastAPI
  5. app = FastAPI()
  6. @app.put("/items/{item_id}")
  7. async def read_items(
  8. item_id: UUID,
  9. start_datetime: Annotated[Union[datetime, None], Body()] = None,
  10. end_datetime: Annotated[Union[datetime, None], Body()] = None,
  11. repeat_at: Annotated[Union[time, None], Body()] = None,
  12. process_after: Annotated[Union[timedelta, None], Body()] = None,
  13. ):
  14. start_process = start_datetime + process_after
  15. duration = end_datetime - start_process
  16. return {
  17. "item_id": item_id,
  18. "start_datetime": start_datetime,
  19. "end_datetime": end_datetime,
  20. "repeat_at": repeat_at,
  21. "process_after": process_after,
  22. "start_process": start_process,
  23. "duration": duration,
  24. }
  1. from datetime import datetime, time, timedelta
  2. from typing import Union
  3. from uuid import UUID
  4. from fastapi import Body, FastAPI
  5. from typing_extensions import Annotated
  6. app = FastAPI()
  7. @app.put("/items/{item_id}")
  8. async def read_items(
  9. item_id: UUID,
  10. start_datetime: Annotated[Union[datetime, None], Body()] = None,
  11. end_datetime: Annotated[Union[datetime, None], Body()] = None,
  12. repeat_at: Annotated[Union[time, None], Body()] = None,
  13. process_after: Annotated[Union[timedelta, None], Body()] = None,
  14. ):
  15. start_process = start_datetime + process_after
  16. duration = end_datetime - start_process
  17. return {
  18. "item_id": item_id,
  19. "start_datetime": start_datetime,
  20. "end_datetime": end_datetime,
  21. "repeat_at": repeat_at,
  22. "process_after": process_after,
  23. "start_process": start_process,
  24. "duration": duration,
  25. }

Tip

尽可能选择使用 Annotated 的版本。

  1. from datetime import datetime, time, timedelta
  2. from uuid import UUID
  3. from fastapi import Body, FastAPI
  4. app = FastAPI()
  5. @app.put("/items/{item_id}")
  6. async def read_items(
  7. item_id: UUID,
  8. start_datetime: datetime | None = Body(default=None),
  9. end_datetime: datetime | None = Body(default=None),
  10. repeat_at: time | None = Body(default=None),
  11. process_after: timedelta | None = Body(default=None),
  12. ):
  13. start_process = start_datetime + process_after
  14. duration = end_datetime - start_process
  15. return {
  16. "item_id": item_id,
  17. "start_datetime": start_datetime,
  18. "end_datetime": end_datetime,
  19. "repeat_at": repeat_at,
  20. "process_after": process_after,
  21. "start_process": start_process,
  22. "duration": duration,
  23. }

Tip

尽可能选择使用 Annotated 的版本。

  1. from datetime import datetime, time, timedelta
  2. from typing import Union
  3. from uuid import UUID
  4. from fastapi import Body, FastAPI
  5. app = FastAPI()
  6. @app.put("/items/{item_id}")
  7. async def read_items(
  8. item_id: UUID,
  9. start_datetime: Union[datetime, None] = Body(default=None),
  10. end_datetime: Union[datetime, None] = Body(default=None),
  11. repeat_at: Union[time, None] = Body(default=None),
  12. process_after: Union[timedelta, None] = Body(default=None),
  13. ):
  14. start_process = start_datetime + process_after
  15. duration = end_datetime - start_process
  16. return {
  17. "item_id": item_id,
  18. "start_datetime": start_datetime,
  19. "end_datetime": end_datetime,
  20. "repeat_at": repeat_at,
  21. "process_after": process_after,
  22. "start_process": start_process,
  23. "duration": duration,
  24. }