Eri*_*ika 5 python pydantic fastapi
我正在使用官方文档使用 FastAPI 构建一个简单的 API,但是当我尝试使用 postman 测试它时,我得到了相同的错误:
pydantic.error_wrappers.ValidationError
Run Code Online (Sandbox Code Playgroud)
这是我的模型:
class Owner(BaseModel):
name: str
address: str
status: int
Run Code Online (Sandbox Code Playgroud)
我的端点:
@app.post('/api/v1/owner/add', response_model=Owner)
async def post_owner(owner: Owner):
return conn.insert_record(settings.DB_TABLE_OWN, owner)
Run Code Online (Sandbox Code Playgroud)
以及我将其插入数据库的方法(RethinkDB)
@staticmethod
def insert_record(table, record):
try:
return DBConnection.DB.table(table).insert(record.json()).run()
except RqlError:
return {'Error': 'Could not insert record'}
Run Code Online (Sandbox Code Playgroud)
这是我使用邮递员发送的 JSON:
{
"name": "Test",
"address": "Test address",
"status": 0
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误如下:
pydantic.error_wrappers.ValidationError: 3 validation errors for Owner
response -> name
field required (type=value_error.missing)
response -> address
field required (type=value_error.missing)
response -> status
field required (type=value_error.missing)
Run Code Online (Sandbox Code Playgroud)
我得说它运行得很好,直到我停止服务器并让它再次运行。
希望你能帮我!
您已设置response_model=OwnerFastAPI 中的结果来验证来自路由的响应post_owner(...)。最有可能的是,它
没有作为所有者conn.insert_record(...)模型返回响应。
response_model一个合适的response_model| 归档时间: |
|
| 查看次数: |
5431 次 |
| 最近记录: |