mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-15 02:07:54 +08:00
✨ Add new method sqlmodel_update()
to update models in place, including an update
parameter for extra data (#804)
This commit is contained in:

committed by
GitHub

parent
7fec884864
commit
fa12c5d87b
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
101
docs_src/tutorial/fastapi/update/tutorial002.py
Normal file
101
docs_src/tutorial/fastapi/update/tutorial002.py
Normal file
@ -0,0 +1,101 @@
|
||||
from typing import List, Optional
|
||||
|
||||
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: Optional[int] = Field(default=None, index=True)
|
||||
|
||||
|
||||
class Hero(HeroBase, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
hashed_password: str = Field()
|
||||
|
||||
|
||||
class HeroCreate(HeroBase):
|
||||
password: str
|
||||
|
||||
|
||||
class HeroRead(HeroBase):
|
||||
id: int
|
||||
|
||||
|
||||
class HeroUpdate(SQLModel):
|
||||
name: Optional[str] = None
|
||||
secret_name: Optional[str] = None
|
||||
age: Optional[int] = None
|
||||
password: Optional[str] = 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)
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
# Use something like passlib here
|
||||
return f"not really hashed {password} hehehe"
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
hashed_password = hash_password(hero.password)
|
||||
with Session(engine) as session:
|
||||
extra_data = {"hashed_password": hashed_password}
|
||||
db_hero = Hero.model_validate(hero, update=extra_data)
|
||||
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.model_dump(exclude_unset=True)
|
||||
extra_data = {}
|
||||
if "password" in hero_data:
|
||||
password = hero_data["password"]
|
||||
hashed_password = hash_password(password)
|
||||
extra_data["hashed_password"] = hashed_password
|
||||
db_hero.sqlmodel_update(hero_data, update=extra_data)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
return db_hero
|
99
docs_src/tutorial/fastapi/update/tutorial002_py310.py
Normal file
99
docs_src/tutorial/fastapi/update/tutorial002_py310.py
Normal file
@ -0,0 +1,99 @@
|
||||
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)
|
||||
hashed_password: str = Field()
|
||||
|
||||
|
||||
class HeroCreate(HeroBase):
|
||||
password: str
|
||||
|
||||
|
||||
class HeroRead(HeroBase):
|
||||
id: int
|
||||
|
||||
|
||||
class HeroUpdate(SQLModel):
|
||||
name: str | None = None
|
||||
secret_name: str | None = None
|
||||
age: int | None = None
|
||||
password: str | 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)
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
# Use something like passlib here
|
||||
return f"not really hashed {password} hehehe"
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
hashed_password = hash_password(hero.password)
|
||||
with Session(engine) as session:
|
||||
extra_data = {"hashed_password": hashed_password}
|
||||
db_hero = Hero.model_validate(hero, update=extra_data)
|
||||
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.model_dump(exclude_unset=True)
|
||||
update_data = {}
|
||||
if "password" in hero_data:
|
||||
password = hero_data["password"]
|
||||
hashed_password = hash_password(password)
|
||||
update_data["hashed_password"] = hashed_password
|
||||
db_hero.sqlmodel_update(hero_data, update=update_data)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
return db_hero
|
101
docs_src/tutorial/fastapi/update/tutorial002_py39.py
Normal file
101
docs_src/tutorial/fastapi/update/tutorial002_py39.py
Normal file
@ -0,0 +1,101 @@
|
||||
from typing import Optional
|
||||
|
||||
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: Optional[int] = Field(default=None, index=True)
|
||||
|
||||
|
||||
class Hero(HeroBase, table=True):
|
||||
id: Optional[int] = Field(default=None, primary_key=True)
|
||||
hashed_password: str = Field()
|
||||
|
||||
|
||||
class HeroCreate(HeroBase):
|
||||
password: str
|
||||
|
||||
|
||||
class HeroRead(HeroBase):
|
||||
id: int
|
||||
|
||||
|
||||
class HeroUpdate(SQLModel):
|
||||
name: Optional[str] = None
|
||||
secret_name: Optional[str] = None
|
||||
age: Optional[int] = None
|
||||
password: Optional[str] = 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)
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
# Use something like passlib here
|
||||
return f"not really hashed {password} hehehe"
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
def on_startup():
|
||||
create_db_and_tables()
|
||||
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
hashed_password = hash_password(hero.password)
|
||||
with Session(engine) as session:
|
||||
extra_data = {"hashed_password": hashed_password}
|
||||
db_hero = Hero.model_validate(hero, update=extra_data)
|
||||
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.model_dump(exclude_unset=True)
|
||||
update_data = {}
|
||||
if "password" in hero_data:
|
||||
password = hero_data["password"]
|
||||
hashed_password = hash_password(password)
|
||||
update_data["hashed_password"] = hashed_password
|
||||
db_hero.sqlmodel_update(hero_data, update=update_data)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
return db_hero
|
Reference in New Issue
Block a user