【发布时间】:2021-05-10 14:20:32
【问题描述】:
当我使用以下代码从 FastAPI 文档运行 put 时,我得到 500 Error: Internal Server Error`` and the terminal shows AttributeError: 'Test' object has no attribute ' items'```` 并且终端显示 AttributeError: 'Test' object has no attribute 'items'。
我可以正常创建、获取、删除等,但由于某种原因我不能随便放。
另外,如果我尝试输入一个不存在的 ID,我通常会收到 404 错误。
如果你能告诉我更多关于它的信息,我将不胜感激。
路由器
@router.put('/{id}', status_code=status.HTTP_202_ACCEPTED)
def update(id, request:schemas.Test ,db:Session = Depends(get_db)):
test= db.query(models.Test).filter(models.Test.id == id)
if not test.first():
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f'Test with id {id} is not found')
test.update(request)
db.commit()
return 'updated'
型号
from sqlalchemy import Column, Integer, String
from db import Base
class Test(Base):
__tablename__ = 'tests'
id = Column('id',Integer, primary_key=True,index=True)
title = Column('title',String(256))
模式
from pydantic import BaseModel
class Test(BaseModel):
title:str
【问题讨论】: