加更改时提交空值不覆盖
This commit is contained in:
@@ -32,6 +32,7 @@ def delete_tea(id:int|None,db=Depends(get_db)):
|
||||
@tea_api.put('/tea/{id}', summary='更新老师信息', description='输入更改的信息,可以为空')
|
||||
def update_tea(id: int, tea: NewTeaRequest = Depends(), db=Depends(get_db)):
|
||||
t = tea.model_dump(exclude_unset=True)
|
||||
print(t)
|
||||
r = update_tea_dao(id=id, t=t, db=db)
|
||||
if r == 'NOT_FOUND':
|
||||
raise HTTPException(status_code=404, detail='老师不存在')
|
||||
@@ -39,6 +40,7 @@ def update_tea(id: int, tea: NewTeaRequest = Depends(), db=Depends(get_db)):
|
||||
raise HTTPException(status_code=400, detail='没有要更新的内容')
|
||||
if isinstance(r, str) and r.startswith('ERROR'):
|
||||
raise HTTPException(status_code=400, detail=r)
|
||||
#先确认是字符串再调用字符串方法判断是不是以错误开头,是就报错给前端
|
||||
return {'detail': '更新成功'}
|
||||
|
||||
#根据任意信息删除
|
||||
|
||||
@@ -39,7 +39,7 @@ def update_tea_dao(id, t, db):
|
||||
if not obj:
|
||||
return 'NOT_FOUND'
|
||||
for k, v in t.items():
|
||||
if hasattr(obj, k):
|
||||
if hasattr(obj, k) and v is not None and v != '':
|
||||
setattr(obj, k, v)
|
||||
db.commit()
|
||||
return 'OK'
|
||||
|
||||
Reference in New Issue
Block a user