Skip to content

Commit bc67b58

Browse files
📝 Update FastAPI tutorial docs to use the new model.sqlmodel_update() instead of old setattr() (#1117)
Co-authored-by: Sebastián RamĂ­rez <[email protected]>
1 parent 990f8f4 commit bc67b58

File tree

17 files changed

+23
-44
lines changed

17 files changed

+23
-44
lines changed

‎docs/contributing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ There is a script that you can run locally to test all the code and generate cov
6363
<div class="termy">
6464

6565
```console
66-
$ bash scripts/test-cov-html.sh
66+
$ bash scripts/test.sh
6767
```
6868

6969
</div>

‎docs/tutorial/fastapi/teams.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Let's now add the **path operations** for teams.
4646

4747
These are equivalent and very similar to the **path operations** for the **heroes** we had before, so we don't have to go over the details for each one, let's check the code.
4848

49-
{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[136:190] hl[136:142,145:153,156:161,164:180,183:190] *}
49+
{* ./docs_src/tutorial/fastapi/teams/tutorial001_py310.py ln[135:188] hl[135:141,144:152,155:160,163:178,181:188] *}
5050

5151
## Using Relationships Attributes
5252

‎docs_src/tutorial/fastapi/app_testing/tutorial001/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ def update_hero(
8888
if not db_hero:
8989
raise HTTPException(status_code=404, detail="Hero not found")
9090
hero_data = hero.model_dump(exclude_unset=True)
91-
for key, value in hero_data.items():
92-
setattr(db_hero, key, value)
91+
db_hero.sqlmodel_update(hero_data)
9392
session.add(db_hero)
9493
session.commit()
9594
session.refresh(db_hero)

‎docs_src/tutorial/fastapi/app_testing/tutorial001_py310/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ def update_hero(
8686
if not db_hero:
8787
raise HTTPException(status_code=404, detail="Hero not found")
8888
hero_data = hero.model_dump(exclude_unset=True)
89-
for key, value in hero_data.items():
90-
setattr(db_hero, key, value)
89+
db_hero.sqlmodel_update(hero_data)
9190
session.add(db_hero)
9291
session.commit()
9392
session.refresh(db_hero)

‎docs_src/tutorial/fastapi/app_testing/tutorial001_py39/main.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ def update_hero(
8888
if not db_hero:
8989
raise HTTPException(status_code=404, detail="Hero not found")
9090
hero_data = hero.model_dump(exclude_unset=True)
91-
for key, value in hero_data.items():
92-
setattr(db_hero, key, value)
91+
db_hero.sqlmodel_update(hero_data)
9392
session.add(db_hero)
9493
session.commit()
9594
session.refresh(db_hero)

‎docs_src/tutorial/fastapi/delete/tutorial001.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
8080
if not db_hero:
8181
raise HTTPException(status_code=404, detail="Hero not found")
8282
hero_data = hero.model_dump(exclude_unset=True)
83-
for key, value in hero_data.items():
84-
setattr(db_hero, key, value)
83+
db_hero.sqlmodel_update(hero_data)
8584
session.add(db_hero)
8685
session.commit()
8786
session.refresh(db_hero)

‎docs_src/tutorial/fastapi/delete/tutorial001_py310.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
7878
if not db_hero:
7979
raise HTTPException(status_code=404, detail="Hero not found")
8080
hero_data = hero.model_dump(exclude_unset=True)
81-
for key, value in hero_data.items():
82-
setattr(db_hero, key, value)
81+
db_hero.sqlmodel_update(hero_data)
8382
session.add(db_hero)
8483
session.commit()
8584
session.refresh(db_hero)

‎docs_src/tutorial/fastapi/delete/tutorial001_py39.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
8080
if not db_hero:
8181
raise HTTPException(status_code=404, detail="Hero not found")
8282
hero_data = hero.model_dump(exclude_unset=True)
83-
for key, value in hero_data.items():
84-
setattr(db_hero, key, value)
83+
db_hero.sqlmodel_update(hero_data)
8584
session.add(db_hero)
8685
session.commit()
8786
session.refresh(db_hero)

‎docs_src/tutorial/fastapi/relationships/tutorial001.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ def update_hero(
126126
if not db_hero:
127127
raise HTTPException(status_code=404, detail="Hero not found")
128128
hero_data = hero.model_dump(exclude_unset=True)
129-
for key, value in hero_data.items():
130-
setattr(db_hero, key, value)
129+
db_hero.sqlmodel_update(hero_data)
131130
session.add(db_hero)
132131
session.commit()
133132
session.refresh(db_hero)
@@ -183,8 +182,7 @@ def update_team(
183182
if not db_team:
184183
raise HTTPException(status_code=404, detail="Team not found")
185184
team_data = team.model_dump(exclude_unset=True)
186-
for key, value in team_data.items():
187-
setattr(db_team, key, value)
185+
db_team.sqlmodel_update(team_data)
188186
session.add(db_team)
189187
session.commit()
190188
session.refresh(db_team)

‎docs_src/tutorial/fastapi/relationships/tutorial001_py310.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ def update_hero(
124124
if not db_hero:
125125
raise HTTPException(status_code=404, detail="Hero not found")
126126
hero_data = hero.model_dump(exclude_unset=True)
127-
for key, value in hero_data.items():
128-
setattr(db_hero, key, value)
127+
db_hero.sqlmodel_update(hero_data)
129128
session.add(db_hero)
130129
session.commit()
131130
session.refresh(db_hero)
@@ -181,8 +180,7 @@ def update_team(
181180
if not db_team:
182181
raise HTTPException(status_code=404, detail="Team not found")
183182
team_data = team.model_dump(exclude_unset=True)
184-
for key, value in team_data.items():
185-
setattr(db_team, key, value)
183+
db_team.sqlmodel_update(team_data)
186184
session.add(db_team)
187185
session.commit()
188186
session.refresh(db_team)

0 commit comments

Comments
 (0)