Sync fastapi docs from eba8942c on 2026-04-11

This commit is contained in:
The Librarian
2026-04-11 04:00:10 +00:00
parent 18a95756ed
commit dea80f32eb
1157 changed files with 15132 additions and 8307 deletions
+13 -13
View File
@@ -6,8 +6,8 @@
### 建立在開放標準的基礎上 { #based-on-open-standards }
* 使用 <a href="https://github.com/OAI/OpenAPI-Specification" class="external-link" target="_blank"><strong>OpenAPI</strong></a> 來建立 API,包含 <dfn title="也稱為:端點、路由">路徑</dfn>、<dfn title="也稱為 HTTP 方法,例如 POST、GET、PUT、DELETE">操作</dfn>、參數、請求內文、安全性等宣告。
* 使用 <a href="https://json-schema.org/" class="external-link" target="_blank"><strong>JSON Schema</strong></a>(因為 OpenAPI 本身就是基於 JSON Schema)自動生成資料模型文件。
* 使用 [**OpenAPI**](https://github.com/OAI/OpenAPI-Specification) 來建立 API,包含 <dfn title="也稱為:端點、路由">路徑</dfn>、<dfn title="也稱為 HTTP 方法,例如 POST、GET、PUT、DELETE">操作</dfn>、參數、請求內文、安全性等宣告。
* 使用 [**JSON Schema**](https://json-schema.org/)(因為 OpenAPI 本身就是基於 JSON Schema)自動生成資料模型文件。
* 經過縝密的研究後圍繞這些標準進行設計,而不是事後在已有系統上附加的一層功能。
* 這也讓我們在多種語言中可以使用自動**用戶端程式碼生成**。
@@ -15,11 +15,11 @@
FastAPI 能生成互動式 API 文件和探索性的 Web 使用者介面。由於該框架基於 OpenAPI,因此有多種選擇,預設提供了兩種。
* <a href="https://github.com/swagger-api/swagger-ui" class="external-link" target="_blank"><strong>Swagger UI</strong></a> 提供互動式探索,讓你可以直接從瀏覽器呼叫並測試你的 API 。
* [**Swagger UI**](https://github.com/swagger-api/swagger-ui) 提供互動式探索,讓你可以直接從瀏覽器呼叫並測試你的 API 。
![Swagger UI interaction](https://fastapi.tiangolo.com/img/index/index-03-swagger-02.png)
* 使用 <a href="https://github.com/Rebilly/ReDoc" class="external-link" target="_blank"><strong>ReDoc</strong></a> 的替代 API 文件。
* 使用 [**ReDoc**](https://github.com/Rebilly/ReDoc) 的替代 API 文件。
![ReDoc](https://fastapi.tiangolo.com/img/index/index-06-redoc-02.png)
@@ -27,7 +27,7 @@ FastAPI 能生成互動式 API 文件和探索性的 Web 使用者介面。由
這一切都基於標準的 **Python 型別**宣告(感謝 Pydantic)。無需學習新的語法,只需使用標準的現代 Python。
如果你需要 2 分鐘來學習如何使用 Python 型別(即使你不使用 FastAPI),可以看看這個簡短的教學:[Python 型別](python-types.md){.internal-link target=_blank}
如果你需要 2 分鐘來學習如何使用 Python 型別(即使你不使用 FastAPI),可以看看這個簡短的教學:[Python 型別](python-types.md)。
如果你寫帶有 Python 型別的程式碼:
@@ -75,7 +75,7 @@ my_second_user: User = User(**second_user_data)
整個框架的設計是為了讓使用變得簡單且直觀,在開始開發之前,所有決策都在多個編輯器上進行了測試,以確保提供最佳的開發體驗。
在最近的 Python 開發者調查中,我們能看到<a href="https://www.jetbrains.com/research/python-developers-survey-2017/#tools-and-features" class="external-link" target="_blank"> 被使用最多的功能是 autocompletion</a>
在最近的 Python 開發者調查中,我們能看到[被使用最多的功能是 autocompletion](https://www.jetbrains.com/research/python-developers-survey-2017/#tools-and-features)
整個 **FastAPI** 框架就是基於這一點,任何地方都可以進行自動補齊。
@@ -83,11 +83,11 @@ my_second_user: User = User(**second_user_data)
在這裡,你的編輯器可能會這樣幫助你:
*<a href="https://code.visualstudio.com/" class="external-link" target="_blank">Visual Studio Code</a> 中:
*[Visual Studio Code](https://code.visualstudio.com/) 中:
![editor support](https://fastapi.tiangolo.com/img/vscode-completion.png)
*<a href="https://www.jetbrains.com/pycharm/" class="external-link" target="_blank">PyCharm</a> 中:
*[PyCharm](https://www.jetbrains.com/pycharm/) 中:
![editor support](https://fastapi.tiangolo.com/img/pycharm-completion.png)
@@ -124,13 +124,13 @@ FastAPI 已經整合了安全性和身份驗證的功能,但不會強制與特
OpenAPI 中定義的安全模式,包括:
* HTTP 基本認證。
* **OAuth2**(也使用 **JWT tokens**)。在 [OAuth2 with JWT](tutorial/security/oauth2-jwt.md){.internal-link target=_blank} 查看教學。
* **OAuth2**(也使用 **JWT tokens**)。在 [OAuth2 with JWT](tutorial/security/oauth2-jwt.md) 查看教學。
* API 密鑰,在:
* 標頭(Header
* 查詢參數
* Cookies,等等。
加上自 Starlette(包括 **session cookie**)的所有安全特性。
加上自 Starlette(包括 **session cookie**)的所有安全特性。
所有的這些都是可重複使用的工具和套件,可以輕鬆與你的系統、資料儲存(Data Stores)、關聯式資料庫(RDBMS)以及非關聯式資料庫(NoSQL)等等整合。
@@ -159,11 +159,11 @@ FastAPI 有一個使用簡單,但是非常強大的 <dfn title='也稱為「co
## Starlette 特性 { #starlette-features }
**FastAPI** 完全相容且基於 <a href="https://www.starlette.dev/" class="external-link" target="_blank"><strong>Starlette</strong></a>。所以,你有其他的 Starlette 程式碼也能正常運作。`FastAPI` 實際上是 `Starlette` 的一個子類別。所以,如果你已經知道或者使用過 Starlette,大部分的功能會以相同的方式運作。
**FastAPI** 完全相容且基於 [**Starlette**](https://www.starlette.dev/)。所以,你有其他的 Starlette 程式碼也能正常運作。`FastAPI` 實際上是 `Starlette` 的一個子類別。所以,如果你已經知道或者使用過 Starlette,大部分的功能會以相同的方式運作。
通過 **FastAPI** 你可以獲得所有 **Starlette** 的特性(FastAPI 就像加強版的 Starlette):
* 性能極其出色。它是 <a href="https://github.com/encode/starlette#performance" class="external-link" target="_blank">Python 可用的最快框架之一,和 **NodeJS****Go** 相當</a>
* 性能極其出色。它是 [Python 可用的最快框架之一,和 **NodeJS** 及 **Go** 相當](https://github.com/encode/starlette#performance)
* **支援 WebSocket**。
* 能在行程內處理背景任務。
* 支援啟動和關閉事件。
@@ -175,7 +175,7 @@ FastAPI 有一個使用簡單,但是非常強大的 <dfn title='也稱為「co
## Pydantic 特性 { #pydantic-features }
**FastAPI** 完全相容且基於 <a href="https://docs.pydantic.dev/" class="external-link" target="_blank"><strong>Pydantic</strong></a>。所以,你有其他 Pydantic 程式碼也能正常工作。
**FastAPI** 完全相容且基於 [**Pydantic**](https://docs.pydantic.dev/)。所以,你有其他 Pydantic 程式碼也能正常工作。
相容包括基於 Pydantic 的外部函式庫,例如用於資料庫的 <abbr title="Object-Relational Mapper - 物件關聯對映器">ORM</abbr>s、<abbr title="Object-Document Mapper - 物件文件對映器">ODM</abbr>s。