FastAPI 依赖注入
什么是依赖注入?, Depends
示例一
# coding: utf8
from fastapi import FastAPI, Depends
from typing import Optional
app = FastAPI()
# 依赖,类似公共组件
async def common_args(
q: Optional[str] = None,
skip: int = 0,
limit: int = 100
):
return {"q": q, "skip": skip, "limit": limit}
@app.get('/items/')
async def get_items(commons: dict = Depends(common_args)):
return commons
@app.get('/users/')
async def get_users(commons: dict = Depends(common_args)):
return commons
# # 启动: uvicorn main:app --reload示例二
嵌套依赖
路径操作装饰器依赖项
全局依赖
FastAPI 兼容性
Last updated