初学fastAPI遇到的问题
value is not a valid dict (type=type_error.dict)
想要在mongodb数据库中实现插入操作
pydandic定义的model:
class Todo(BaseModel):
title:str
description:str
数据库代码:
async def create_todo(todo):
document = todo
result = await collection.insert_one(document.dict())
return result
后端代码:
@app.post("/api/todo",response_model=Todo)
async def post_todo(todo:Todo):
response = await create_todo(todo)
print(response)
print(todo.dict())
if response:
return response
raise HTTPException(400,"Bad request")
数据库可以进行正常的插入操作,但是fastapi提示500错误:
pydantic.error_wrappers.ValidationError: 1 validation error for Todo
response
value is not a valid dict (type=type_error.dict)
请问这种问题是什么原因,怎么解决?
在类Todo里面实现dict方法
response = await create_todo(todo)这句response应该是一个字典
请贴完整代码,好修改