如何解决在 GET 请求中使用 pydantic 验证器
我正在尝试验证数据库中已有的数据。
我有这个 Pydantic 模型:
class Schema(BaseModel):
name: str
@validator("name")
def name_must_not_contain_spaces(cls,v):
if ' ' in v:
raise ValueError("must not contain space")
return v
这是返回所有对象的 crud 操作的 get_all
:
# crud.py
async def get_all() -> List:
objects = await TestModel.all().values()
return objects
# TestModel is a Model created with Tortoise-orm.
这是 GET 端点:
@router.get('/',response_model=List[Schema])
async def get_all_tests() -> List[Schema]:
return await crud.get_all()
当我尝试 POST 时,它运行良好,在 json 中返回引发的错误。
但是,当我尝试验证数据库中已有的数据时,出现内部服务器错误,并且在终端上有此引用:
ERROR:uvicorn.error:Exception in Asgi application
Traceback (most recent call last):
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py",line 396,in run_asgi
result = await app(self.scope,self.receive,self.send)
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py",line 45,in __call__
return await self.app(scope,receive,send)
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/fastapi/applications.py",line 199,in __call__
await super().__call__(scope,send)
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/applications.py",line 112,in __call__
await self.middleware_stack(scope,send)
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py",line 181,in __call__
raise exc from None
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py",line 159,in __call__
await self.app(scope,_send)
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/exceptions.py",line 82,in __call__
raise exc from None
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/exceptions.py",line 71,sender)
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/routing.py",line 580,in __call__
await route.handle(scope,send)
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/routing.py",line 241,in handle
await self.app(scope,line 52,in app
response = await func(request)
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/fastapi/routing.py",line 209,in app
response_data = await serialize_response(
File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/fastapi/routing.py",line 126,in serialize_response
raise ValidationError(errors,field.type_)
pydantic.error_wrappers.ValidationError: 1 validation error for Schema
response -> 5 -> name
must not contain space (type=value_error)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。