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()):