Khi bạn cần gửi dữ liệu từ ứng dụng khách [giả sử là trình duyệt] tới API của mình, bạn gửi dữ liệu đó dưới dạng nội dung yêu cầu
Nội dung yêu cầu là dữ liệu do khách hàng gửi tới API của bạn. Nội dung phản hồi là dữ liệu mà API của bạn gửi tới ứng dụng khách
API của bạn hầu như luôn phải gửi nội dung phản hồi. Nhưng khách hàng không nhất thiết phải gửi các cơ quan yêu cầu mọi lúc
Để khai báo một nội dung yêu cầu, bạn sử dụng các mô hình Pydantic với tất cả sức mạnh và lợi ích của chúng
Thông tin
Để gửi dữ liệu, bạn nên sử dụng một trong.
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
4 [phổ biến hơn], from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
5, from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
6 hoặc from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
7Gửi nội dung có yêu cầu
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
8 có hành vi không xác định trong thông số kỹ thuật, tuy nhiên, nó được hỗ trợ bởi FastAPI, chỉ dành cho các trường hợp sử dụng rất phức tạp/cực đoanVì nó không được khuyến khích, các tài liệu tương tác với giao diện người dùng Swagger sẽ không hiển thị tài liệu cho phần thân khi sử dụng
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
8 và các proxy ở giữa có thể không hỗ trợ nóNhập khẩu from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
0¶ của Pydantic
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
Trước tiên, bạn cần nhập
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
0 từ from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
2Trăn 3. 6 trở lênPython 3. 10 trở lên
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
Tạo mô hình dữ liệu của bạn¶
Sau đó, bạn khai báo mô hình dữ liệu của mình dưới dạng một lớp kế thừa từ
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
0Sử dụng các loại Python tiêu chuẩn cho tất cả các thuộc tính
Trăn 3. 6 trở lênPython 3. 10 trở lên
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
3Cũng giống như khi khai báo tham số truy vấn, khi thuộc tính model có giá trị mặc định thì không bắt buộc. Nếu không, nó được yêu cầu. Sử dụng
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
4 để biến nó thành tùy chọnVí dụ: mô hình ở trên khai báo một JSON "
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
5" [hoặc Python from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
6] nhưfrom fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
7vì
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
7 và from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
8 là tùy chọn [với giá trị mặc định là from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
4], JSON "from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
5" này cũng sẽ hợp lệfrom fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
2Khai báo nó như một tham số¶
Để thêm nó vào hoạt động đường dẫn của bạn, hãy khai báo nó giống như cách bạn đã khai báo các tham số truy vấn và đường dẫn
Trăn 3. 6 trở lênPython 3. 10 trở lên
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
3from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
4và khai báo kiểu của nó như mô hình bạn đã tạo,
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
31Kết quả¶
Chỉ với khai báo kiểu Python đó, FastAPI sẽ
- Đọc phần thân của yêu cầu dưới dạng JSON
- Chuyển đổi các loại tương ứng [nếu cần]
- Xác thực dữ liệu
- Nếu dữ liệu không hợp lệ, nó sẽ trả về một lỗi đẹp và rõ ràng, cho biết chính xác dữ liệu không chính xác ở đâu và như thế nào
- Cung cấp cho bạn dữ liệu nhận được trong tham số
32from fastapi import FastAPI from pydantic import BaseModel class Item[BaseModel]: name: str description: str | None = None price: float tax: float | None = None app = FastAPI[] @app.post["/items/"] async def create_item[item: Item]: return item
- Như bạn đã khai báo trong hàm là loại
31, bạn cũng sẽ có tất cả hỗ trợ của trình chỉnh sửa [hoàn thành, v.v.] cho tất cả các thuộc tính và loại của chúngfrom fastapi import FastAPI from pydantic import BaseModel class Item[BaseModel]: name: str description: str | None = None price: float tax: float | None = None app = FastAPI[] @app.post["/items/"] async def create_item[item: Item]: return item
- Như bạn đã khai báo trong hàm là loại
- Tạo các định nghĩa Lược đồ JSON cho mô hình của bạn, bạn cũng có thể sử dụng chúng ở bất kỳ nơi nào khác mà bạn muốn nếu nó phù hợp với dự án của bạn
- Các lược đồ đó sẽ là một phần của lược đồ OpenAPI được tạo và được sử dụng bởi giao diện người dùng tài liệu tự động
Tài liệu tự động¶
Lược đồ JSON của các mô hình của bạn sẽ là một phần của lược đồ do OpenAPI tạo ra và sẽ được hiển thị trong tài liệu API tương tác
Và cũng sẽ được sử dụng trong các tài liệu API bên trong mỗi thao tác đường dẫn cần chúng
Hỗ trợ biên tập¶
Trong trình chỉnh sửa của bạn, bên trong chức năng của bạn, bạn sẽ nhận được các gợi ý về loại và hoàn thành ở mọi nơi [điều này sẽ không xảy ra nếu bạn nhận được một
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: Union[str, None] = None
price: float
tax: Union[float, None] = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
6 thay vì một mô hình Pydantic]Bạn cũng nhận được kiểm tra lỗi cho các hoạt động loại không chính xác
Đây không phải là tình cờ, toàn bộ khung được xây dựng xung quanh thiết kế đó
Và nó đã được kiểm tra kỹ lưỡng ở giai đoạn thiết kế, trước khi triển khai, để đảm bảo nó sẽ hoạt động với tất cả các trình chỉnh sửa
Thậm chí còn có một số thay đổi đối với chính Pydantic để hỗ trợ điều này
Các ảnh chụp màn hình trước được chụp bằng Visual Studio Code
Nhưng bạn sẽ nhận được hỗ trợ trình chỉnh sửa tương tự với PyCharm và hầu hết các trình chỉnh sửa Python khác
Mẹo
Nếu bạn sử dụng PyCharm làm trình chỉnh sửa của mình, bạn có thể sử dụng Plugin Pydantic PyCharm
Nó cải thiện hỗ trợ trình chỉnh sửa cho các mô hình Pydantic, với
- tự động hoàn thành
- loại kiểm tra
- tái cấu trúc
- đang tìm kiếm
- thanh tra
Sử dụng mô hình¶
Bên trong hàm, bạn có thể truy cập trực tiếp tất cả các thuộc tính của đối tượng mô hình
Trăn 3. 6 trở lênPython 3. 10 trở lên
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
9from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
0Nội dung yêu cầu + tham số đường dẫn¶
Bạn có thể khai báo các tham số đường dẫn và nội dung yêu cầu cùng một lúc
FastAPI sẽ nhận ra rằng các tham số hàm khớp với tham số đường dẫn phải được lấy từ đường dẫn và các tham số hàm được khai báo là mô hình Pydantic phải được lấy từ phần thân yêu cầu
Trăn 3. 6 trở lênPython 3. 10 trở lên
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
0from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
1Nội dung yêu cầu + đường dẫn + tham số truy vấn¶
Bạn cũng có thể khai báo các tham số nội dung, đường dẫn và truy vấn, tất cả cùng một lúc
FastAPI sẽ nhận ra từng người trong số họ và lấy dữ liệu từ đúng nơi
Trăn 3. 6 trở lênPython 3. 10 trở lên
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
2from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
3Các tham số chức năng sẽ được công nhận như sau
- Nếu tham số cũng được khai báo trong đường dẫn, nó sẽ được sử dụng làm tham số đường dẫn
- Nếu tham số thuộc loại số ít [như
35,from fastapi import FastAPI from pydantic import BaseModel class Item[BaseModel]: name: str description: str | None = None price: float tax: float | None = None app = FastAPI[] @app.post["/items/"] async def create_item[item: Item]: return item
36,from fastapi import FastAPI from pydantic import BaseModel class Item[BaseModel]: name: str description: str | None = None price: float tax: float | None = None app = FastAPI[] @app.post["/items/"] async def create_item[item: Item]: return item
37,from fastapi import FastAPI from pydantic import BaseModel class Item[BaseModel]: name: str description: str | None = None price: float tax: float | None = None app = FastAPI[] @app.post["/items/"] async def create_item[item: Item]: return item
38, v.v.] thì tham số đó sẽ được hiểu là tham số truy vấnfrom fastapi import FastAPI from pydantic import BaseModel class Item[BaseModel]: name: str description: str | None = None price: float tax: float | None = None app = FastAPI[] @app.post["/items/"] async def create_item[item: Item]: return item
- Nếu tham số được khai báo thuộc loại mô hình Pydantic, nó sẽ được hiểu là phần thân yêu cầu
Ghi chú
FastAPI sẽ biết rằng giá trị của
from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
39 là không bắt buộc vì giá trị mặc định là from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
70from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
71 trong from fastapi import FastAPI
from pydantic import BaseModel
class Item[BaseModel]:
name: str
description: str | None = None
price: float
tax: float | None = None
app = FastAPI[]
@app.post["/items/"]
async def create_item[item: Item]:
return item
72 không được FastAPI sử dụng, nhưng sẽ cho phép trình chỉnh sửa của bạn hỗ trợ bạn tốt hơn và phát hiện lỗiKhông có Pydantic¶
Nếu bạn không muốn sử dụng các mô hình Pydantic, bạn cũng có thể sử dụng các tham số Body. Xem tài liệu về Body - Multiple Parameters. Giá trị số ít trong cơ thể