为何会产生下面的错误?改如何更改?File "/home/zhangshikun/work/57/mit/mit-service/src/http/routes/image/tag.py", line 84, in <module> async def img_tag( File "/home/zhangshikun/anaconda3/lib/python3.8/site-packages/fastapi/routing.py", line 582, in decorator self.add_api_route( File "/home/zhangshikun/anaconda3/lib/python3.8/site-packages/fastapi/routing.py", line 525, in add_api_route route = route_class( File "/home/zhangshikun/anaconda3/lib/python3.8/site-packages/fastapi/routing.py", line 407, in __init__ self.dependant = get_dependant(path=self.path_format, call=self.endpoint) File "/home/zhangshikun/anaconda3/lib/python3.8/site-packages/fastapi/dependencies/utils.py", line 324, in get_dependant add_param_to_fields(field=param_field, dependant=dependant) File "/home/zhangshikun/anaconda3/lib/python3.8/site-packages/fastapi/dependencies/utils.py", line 413, in add_param_to_fields if field_info.in_ == params.ParamTypes.path: AttributeError: 'FieldInfo' object has no attribute 'in_'
时间: 2023-07-01 08:19:58 浏览: 213
测风数据导出
这个错误是由于FastAPI版本更改引起的。在较老的FastAPI版本中,`FieldInfo`对象有一个`in_`属性,可以用来指示一个参数是在查询字符串、请求体、还是路径中。然而,较新的FastAPI版本中,`FieldInfo`对象的`in_`属性已被移除,而是由一个新的`Param`对象的`in_`属性来代替。
要解决这个问题,你需要检查你的FastAPI版本,如果是较新的版本,你需要更新你的代码:
1. 导入`Param`类:
```
from fastapi.params import Param
```
2. 修改`add_param_to_fields`函数中的代码,将`field_info.in_`替换为`field_info.param.in_`:
```
def add_param_to_fields(field: Field, dependant: Dependant) -> None:
...
if field_info.param.in_ == ParamTypes.path:
...
elif field_info.param.in_ == ParamTypes.query:
...
elif field_info.param.in_ == ParamTypes.header:
...
...
```
这样就可以解决这个问题了。
阅读全文