首页 > 其他分享 >FastAPI报错:AttributeError 'Depends' object has no attribute 'query'

FastAPI报错:AttributeError 'Depends' object has no attribute 'query'

时间:2022-11-10 17:33:29浏览次数:47  
标签:get FastAPI no db Depends Session 报错 str

FastAPI报错:AttributeError: 'Depends' object has no attribute 'query'

简单来说,这个问题是由于在不属于路径操作的函数下,使用db: Session = Depends(get_db)导致的。

例如,如果这么编写函数

def authenticate_user(phone: str, password: str, db: Session =Depends(get_db)):

就会导致这个问题。

那怎么解决呢?

网上的解决方案:

只能在路径操作的参数里获取Session,然后一层层传递到所需要的函数里。我试过,没有成功,不知道是我技术不够还是其他原因。

我的解决方法:

将Depends换成next,get_db换成get_db()方法,即可解决。

def authenticate_user(phone: str, password: str, db: Session =next(get_db()):


标签:get,FastAPI,no,db,Depends,Session,报错,str
From: https://blog.51cto.com/huangzequn/5841895

相关文章