📝 Update FastAPI tutorial docs to use the new model.sqlmodel_update() instead of old setattr() (#1117)

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
Joel Pérez Izquierdo
2025-04-27 14:02:41 +01:00
committed by GitHub
parent 990f8f407f
commit bc67b582b6
17 changed files with 23 additions and 44 deletions

View File

@ -80,8 +80,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
if not db_hero:
raise HTTPException(status_code=404, detail="Hero not found")
hero_data = hero.model_dump(exclude_unset=True)
for key, value in hero_data.items():
setattr(db_hero, key, value)
db_hero.sqlmodel_update(hero_data)
session.add(db_hero)
session.commit()
session.refresh(db_hero)

View File

@ -78,8 +78,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
if not db_hero:
raise HTTPException(status_code=404, detail="Hero not found")
hero_data = hero.model_dump(exclude_unset=True)
for key, value in hero_data.items():
setattr(db_hero, key, value)
db_hero.sqlmodel_update(hero_data)
session.add(db_hero)
session.commit()
session.refresh(db_hero)

View File

@ -80,8 +80,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
if not db_hero:
raise HTTPException(status_code=404, detail="Hero not found")
hero_data = hero.model_dump(exclude_unset=True)
for key, value in hero_data.items():
setattr(db_hero, key, value)
db_hero.sqlmodel_update(hero_data)
session.add(db_hero)
session.commit()
session.refresh(db_hero)