Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update base.py
更新crud update方法
  • Loading branch information
4linuxfun committed Feb 18, 2024
commit 9bb28cc7aa380aa6d08d1190cf1a6a8de2c34ba4
12 changes: 8 additions & 4 deletions server/crud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ def insert(self, db: Session, obj_in):
return obj_in

def update(self, db: Session, db_obj: ModelType, new_obj: ModelType):
# SQLModel直接使用的pydantic的dict方法,没有轮询SQLModel封装的__sqlmodel_relationships__,对于外键的更新,只能手动指定
update_date = new_obj.dict()
"""
使用sqlmodel新函数sqlmodel_update更新
:param db:
:param db_obj:
:param new_obj:
"""
update_date = new_obj.model_dump(exclude_unset=True)
logger.debug(update_date)
for field in update_date:
setattr(db_obj, field, update_date[field])
db_obj.sqlmodel_update(update_date)
db.add(db_obj)
db.commit()
db.refresh(db_obj)
Expand Down