MCPcopy Index your code
hub / github.com/fastapi/sqlmodel / update_hero

Function update_hero

docs_src/tutorial/fastapi/update/tutorial002_py310.py:84–99  ·  view source on GitHub ↗
(hero_id: int, hero: HeroUpdate)

Source from the content-addressed store, hash-verified

82
83@app.patch("/heroes/{hero_id}", response_model=HeroPublic)
84def update_hero(hero_id: int, hero: HeroUpdate):
85 with Session(engine) as session:
86 db_hero = session.get(Hero, hero_id)
87 if not db_hero:
88 raise HTTPException(status_code=404, detail="Hero not found")
89 hero_data = hero.model_dump(exclude_unset=True)
90 extra_data = {}
91 if "password" in hero_data:
92 password = hero_data["password"]
93 hashed_password = hash_password(password)
94 extra_data["hashed_password"] = hashed_password
95 db_hero.sqlmodel_update(hero_data, update=extra_data)
96 session.add(db_hero)
97 session.commit()
98 session.refresh(db_hero)
99 return db_hero

Callers

nothing calls this directly

Calls 4

SessionClass · 0.90
hash_passwordFunction · 0.85
model_dumpMethod · 0.80
sqlmodel_updateMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…