【发布时间】:2021-07-03 13:18:29
【问题描述】:
我正在学习 fastAPI,但不知道如何部分更新用户信息。给定的解决方案是设置exclude_unset=True,但我不知道在哪里写。这是我的代码:
路由器/user.py:
@router.patch('/{id}', status_code=status.HTTP_202_ACCEPTED)
def update_user(id, request: sUser, db: Session = Depends(get_db)):
user = db.query(mUser).filter(mUser.id == id)
if not user.first():
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f'The User with the id {id} is not found')
user.update(request.dict(exclude={'createdAt'}, exclude_unset=True))
db.commit()
return user.first()
PS exclude = {'createdAt'} 有效,但 exclude_unset=True 无效..
这是我的用户架构:
schemas.py
class User(BaseModel):
username: str
dob: datetime.date
password: str
createdAt: datetime.datetime
【问题讨论】: