Coverage for docs_src/tutorial/fastapi/update/tutorial002.py: 100%
70 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-16 00:02 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-16 00:02 +0000
1from typing import Annotated 1abcdefghijklm
3from fastapi import FastAPI, HTTPException, Query 1abcdefghijklm
4from sqlmodel import Field, Session, SQLModel, create_engine, select 1abcdefghijklm
7class HeroBase(SQLModel): 1abcdefghijklm
8 name: str = Field(index=True) 1abcdefghijklm
9 secret_name: str 1abcdefghijklm
10 age: int | None = Field(default=None, index=True) 1abcdefghijklm
13class Hero(HeroBase, table=True): 1abcdefghijklm
14 id: int | None = Field(default=None, primary_key=True) 1abcdefghijklm
15 hashed_password: str = Field() 1abcdefghijklm
18class HeroCreate(HeroBase): 1abcdefghijklm
19 password: str 1abcdefghijklm
22class HeroPublic(HeroBase): 1abcdefghijklm
23 id: int 1abcdefghijklm
26class HeroUpdate(SQLModel): 1abcdefghijklm
27 name: str | None = None 1abcdefghijklm
28 secret_name: str | None = None 1abcdefghijklm
29 age: int | None = None 1abcdefghijklm
30 password: str | None = None 1abcdefghijklm
33sqlite_file_name = "database.db" 1abcdefghijklm
34sqlite_url = f"sqlite:///{sqlite_file_name}" 1abcdefghijklm
36connect_args = {"check_same_thread": False} 1abcdefghijklm
37engine = create_engine(sqlite_url, echo=True, connect_args=connect_args) 1abcdefghijklm
40def create_db_and_tables(): 1abcdefghijklm
41 SQLModel.metadata.create_all(engine) 1nopqrstuvwxyz
44def hash_password(password: str) -> str: 1abcdefghijklm
45 # Use something like passlib here
46 return f"not really hashed {password} hehehe" 1nopqrstuvwxyz
49app = FastAPI() 1abcdefghijklm
52@app.on_event("startup") 1abcdefghijklm
53def on_startup(): 1abcdefghijklm
54 create_db_and_tables() 1nopqrstuvwxyz
57@app.post("/heroes/", response_model=HeroPublic) 1abcdefghijklm
58def create_hero(hero: HeroCreate): 1abcdefghijklm
59 hashed_password = hash_password(hero.password) 1nopqrstuvwxyz
60 with Session(engine) as session: 1nopqrstuvwxyz
61 extra_data = {"hashed_password": hashed_password} 1nopqrstuvwxyz
62 db_hero = Hero.model_validate(hero, update=extra_data) 1nopqrstuvwxyz
63 session.add(db_hero) 1nopqrstuvwxyz
64 session.commit() 1nopqrstuvwxyz
65 session.refresh(db_hero) 1nopqrstuvwxyz
66 return db_hero 1nopqrstuvwxyz
69@app.get("/heroes/", response_model=list[HeroPublic]) 1abcdefghijklm
70def read_heroes(offset: int = 0, limit: Annotated[int, Query(le=100)] = 100): 1abcdefghijklm
71 with Session(engine) as session: 1nopqrstuvwxyz
72 heroes = session.exec(select(Hero).offset(offset).limit(limit)).all() 1nopqrstuvwxyz
73 return heroes 1nopqrstuvwxyz
76@app.get("/heroes/{hero_id}", response_model=HeroPublic) 1abcdefghijklm
77def read_hero(hero_id: int): 1abcdefghijklm
78 with Session(engine) as session: 1nopqrstuvwxyz
79 hero = session.get(Hero, hero_id) 1nopqrstuvwxyz
80 if not hero: 1nopqrstuvwxyz
81 raise HTTPException(status_code=404, detail="Hero not found") 1nopqrstuvwxyz
82 return hero 1nopqrstuvwxyz
85@app.patch("/heroes/{hero_id}", response_model=HeroPublic) 1abcdefghijklm
86def update_hero(hero_id: int, hero: HeroUpdate): 1abcdefghijklm
87 with Session(engine) as session: 1nopqrstuvwxyz
88 db_hero = session.get(Hero, hero_id) 1nopqrstuvwxyz
89 if not db_hero: 1nopqrstuvwxyz
90 raise HTTPException(status_code=404, detail="Hero not found") 1nopqrstuvwxyz
91 hero_data = hero.model_dump(exclude_unset=True) 1nopqrstuvwxyz
92 extra_data = {} 1nopqrstuvwxyz
93 if "password" in hero_data: 1nopqrstuvwxyz
94 password = hero_data["password"] 1nopqrstuvwxyz
95 hashed_password = hash_password(password) 1nopqrstuvwxyz
96 extra_data["hashed_password"] = hashed_password 1nopqrstuvwxyz
97 db_hero.sqlmodel_update(hero_data, update=extra_data) 1nopqrstuvwxyz
98 session.add(db_hero) 1nopqrstuvwxyz
99 session.commit() 1nopqrstuvwxyz
100 session.refresh(db_hero) 1nopqrstuvwxyz
101 return db_hero 1nopqrstuvwxyz