mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-15 10:11:34 +08:00
📝 Add source examples for Python 3.9 and 3.10 (#715)
* 📝 Add source examples for Python 3.9 and 3.10 * ✅ Add tests for new source examples for Python 3.9 and 3.10, still needs pytest markers * ✅ Add tests for fastapi examples * ✅ Update tests for FastAPI app testing, for Python 3.9 and 3.10, fixing multi-app testing conflicts * ✅ Require Python 3.9 and 3.10 for tests * ✅ Update tests with missing markers
This commit is contained in:

committed by
GitHub

parent
cce30d7546
commit
d8effcbc5c
97
docs_src/tutorial/fastapi/delete/tutorial001_py310.py
Normal file
97
docs_src/tutorial/fastapi/delete/tutorial001_py310.py
Normal file
@ -0,0 +1,97 @@
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from sqlmodel import Field, Session, SQLModel, create_engine, select
|
||||
|
||||
|
||||
class HeroBase(SQLModel):
|
||||
name: str = Field(index=True)
|
||||
secret_name: str
|
||||
age: int | None = Field(default=None, index=True)
|
||||
|
||||
|
||||
class Hero(HeroBase, table=True):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
|
||||
class HeroCreate(HeroBase):
|
||||
pass
|
||||
|
||||
|
||||
class HeroRead(HeroBase):
|
||||
id: int
|
||||
|
||||
|
||||
class HeroUpdate(SQLModel):
|
||||
name: str | None = None
|
||||
secret_name: str | None = None
|
||||
age: int | None = None
|
||||
|
||||
|
||||
sqlite_file_name = "database.db"
|
||||
sqlite_url = f"sqlite:///{sqlite_file_name}"
|
||||
|
||||
connect_args = {"check_same_thread": False}
|
||||
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
|
||||
|
||||
|
||||
def create_db_and_tables():
|
||||
SQLModel.metadata.create_all(engine)
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
return db_hero
|
||||
|
||||
|
||||
@app.get("/heroes/", response_model=list[HeroRead])
|
||||
def read_heroes(offset: int = 0, limit: int = Query(default=100, le=100)):
|
||||
with Session(engine) as session:
|
||||
heroes = session.exec(select(Hero).offset(offset).limit(limit)).all()
|
||||
return heroes
|
||||
|
||||
|
||||
@app.get("/heroes/{hero_id}", response_model=HeroRead)
|
||||
def read_hero(hero_id: int):
|
||||
with Session(engine) as session:
|
||||
hero = session.get(Hero, hero_id)
|
||||
if not hero:
|
||||
raise HTTPException(status_code=404, detail="Hero not found")
|
||||
return hero
|
||||
|
||||
|
||||
@app.patch("/heroes/{hero_id}", response_model=HeroRead)
|
||||
def update_hero(hero_id: int, hero: HeroUpdate):
|
||||
with Session(engine) as session:
|
||||
db_hero = session.get(Hero, hero_id)
|
||||
if not db_hero:
|
||||
raise HTTPException(status_code=404, detail="Hero not found")
|
||||
hero_data = hero.dict(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
return db_hero
|
||||
|
||||
|
||||
@app.delete("/heroes/{hero_id}")
|
||||
def delete_hero(hero_id: int):
|
||||
with Session(engine) as session:
|
||||
hero = session.get(Hero, hero_id)
|
||||
if not hero:
|
||||
raise HTTPException(status_code=404, detail="Hero not found")
|
||||
session.delete(hero)
|
||||
session.commit()
|
||||
return {"ok": True}
|
Reference in New Issue
Block a user