所以我试图在这里编写简单的函数,但是每次我运行 swagger 时,都会出现上述错误。
这是我的功能:
def authenticate_user(username: str, password: str, db: Session = Depends(bd.get_db)):
user = db.query(bd.User.username).filter(username == username).first()
if not user:
return False
if not verify_password(password, user.password_hash):
return False
return user
Run Code Online (Sandbox Code Playgroud)
这是我的 get_db 函数,它非常标准:
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
Run Code Online (Sandbox Code Playgroud)
我注意到它Depends(bd.get_db)在端点函数(带有@app.post/@app.get装饰器的函数)中工作得很好,但在普通函数中却不起作用。
显然,我不太理解依赖注入的概念,但我还不能完全理解它。