mirror of
https://github.com/fastapi/sqlmodel.git
synced 2025-08-15 18:28:12 +08:00
✨ Add support for Pydantic v2 (while keeping support for v1 if v2 is not available), including initial work by AntonDeMeester (#722)
Co-authored-by: Mohamed Farahat <farahats9@yahoo.com> Co-authored-by: Stefan Borer <stefan.borer@gmail.com> Co-authored-by: Peter Landry <peter.landry@gmail.com> Co-authored-by: Anton De Meester <antondemeester+github@gmail.com>
This commit is contained in:

committed by
GitHub

parent
5b733b348d
commit
fa2f178b8a
@ -54,7 +54,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -87,7 +87,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -52,7 +52,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -85,7 +85,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -54,7 +54,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -87,7 +87,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -50,7 +50,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -79,7 +79,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -48,7 +48,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -77,7 +77,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -50,7 +50,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -79,7 +79,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -44,7 +44,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -42,7 +42,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -44,7 +44,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -46,7 +46,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -44,7 +44,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -46,7 +46,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -44,7 +44,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -42,7 +42,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -44,7 +44,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -44,7 +44,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -42,7 +42,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -44,7 +44,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
|
@ -92,7 +92,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -125,7 +125,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
@ -146,7 +146,7 @@ def delete_hero(*, session: Session = Depends(get_session), hero_id: int):
|
||||
|
||||
@app.post("/teams/", response_model=TeamRead)
|
||||
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
|
||||
db_team = Team.from_orm(team)
|
||||
db_team = Team.model_validate(team)
|
||||
session.add(db_team)
|
||||
session.commit()
|
||||
session.refresh(db_team)
|
||||
@ -182,7 +182,7 @@ def update_team(
|
||||
db_team = session.get(Team, team_id)
|
||||
if not db_team:
|
||||
raise HTTPException(status_code=404, detail="Team not found")
|
||||
team_data = team.dict(exclude_unset=True)
|
||||
team_data = team.model_dump(exclude_unset=True)
|
||||
for key, value in team_data.items():
|
||||
setattr(db_team, key, value)
|
||||
session.add(db_team)
|
||||
|
@ -90,7 +90,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -123,7 +123,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
@ -144,7 +144,7 @@ def delete_hero(*, session: Session = Depends(get_session), hero_id: int):
|
||||
|
||||
@app.post("/teams/", response_model=TeamRead)
|
||||
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
|
||||
db_team = Team.from_orm(team)
|
||||
db_team = Team.model_validate(team)
|
||||
session.add(db_team)
|
||||
session.commit()
|
||||
session.refresh(db_team)
|
||||
@ -180,7 +180,7 @@ def update_team(
|
||||
db_team = session.get(Team, team_id)
|
||||
if not db_team:
|
||||
raise HTTPException(status_code=404, detail="Team not found")
|
||||
team_data = team.dict(exclude_unset=True)
|
||||
team_data = team.model_dump(exclude_unset=True)
|
||||
for key, value in team_data.items():
|
||||
setattr(db_team, key, value)
|
||||
session.add(db_team)
|
||||
|
@ -92,7 +92,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -125,7 +125,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
@ -146,7 +146,7 @@ def delete_hero(*, session: Session = Depends(get_session), hero_id: int):
|
||||
|
||||
@app.post("/teams/", response_model=TeamRead)
|
||||
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
|
||||
db_team = Team.from_orm(team)
|
||||
db_team = Team.model_validate(team)
|
||||
session.add(db_team)
|
||||
session.commit()
|
||||
session.refresh(db_team)
|
||||
@ -182,7 +182,7 @@ def update_team(
|
||||
db_team = session.get(Team, team_id)
|
||||
if not db_team:
|
||||
raise HTTPException(status_code=404, detail="Team not found")
|
||||
team_data = team.dict(exclude_unset=True)
|
||||
team_data = team.model_dump(exclude_unset=True)
|
||||
for key, value in team_data.items():
|
||||
setattr(db_team, key, value)
|
||||
session.add(db_team)
|
||||
|
@ -54,7 +54,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -87,7 +87,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -52,7 +52,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -85,7 +85,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -54,7 +54,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -87,7 +87,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -83,7 +83,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -116,7 +116,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
@ -137,7 +137,7 @@ def delete_hero(*, session: Session = Depends(get_session), hero_id: int):
|
||||
|
||||
@app.post("/teams/", response_model=TeamRead)
|
||||
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
|
||||
db_team = Team.from_orm(team)
|
||||
db_team = Team.model_validate(team)
|
||||
session.add(db_team)
|
||||
session.commit()
|
||||
session.refresh(db_team)
|
||||
@ -173,7 +173,7 @@ def update_team(
|
||||
db_team = session.get(Team, team_id)
|
||||
if not db_team:
|
||||
raise HTTPException(status_code=404, detail="Team not found")
|
||||
team_data = team.dict(exclude_unset=True)
|
||||
team_data = team.model_dump(exclude_unset=True)
|
||||
for key, value in team_data.items():
|
||||
setattr(db_team, key, value)
|
||||
session.add(db_team)
|
||||
|
@ -81,7 +81,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -114,7 +114,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
@ -135,7 +135,7 @@ def delete_hero(*, session: Session = Depends(get_session), hero_id: int):
|
||||
|
||||
@app.post("/teams/", response_model=TeamRead)
|
||||
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
|
||||
db_team = Team.from_orm(team)
|
||||
db_team = Team.model_validate(team)
|
||||
session.add(db_team)
|
||||
session.commit()
|
||||
session.refresh(db_team)
|
||||
@ -171,7 +171,7 @@ def update_team(
|
||||
db_team = session.get(Team, team_id)
|
||||
if not db_team:
|
||||
raise HTTPException(status_code=404, detail="Team not found")
|
||||
team_data = team.dict(exclude_unset=True)
|
||||
team_data = team.model_dump(exclude_unset=True)
|
||||
for key, value in team_data.items():
|
||||
setattr(db_team, key, value)
|
||||
session.add(db_team)
|
||||
|
@ -83,7 +83,7 @@ def on_startup():
|
||||
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -116,7 +116,7 @@ def update_hero(
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
@ -137,7 +137,7 @@ def delete_hero(*, session: Session = Depends(get_session), hero_id: int):
|
||||
|
||||
@app.post("/teams/", response_model=TeamRead)
|
||||
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
|
||||
db_team = Team.from_orm(team)
|
||||
db_team = Team.model_validate(team)
|
||||
session.add(db_team)
|
||||
session.commit()
|
||||
session.refresh(db_team)
|
||||
@ -173,7 +173,7 @@ def update_team(
|
||||
db_team = session.get(Team, team_id)
|
||||
if not db_team:
|
||||
raise HTTPException(status_code=404, detail="Team not found")
|
||||
team_data = team.dict(exclude_unset=True)
|
||||
team_data = team.model_dump(exclude_unset=True)
|
||||
for key, value in team_data.items():
|
||||
setattr(db_team, key, value)
|
||||
session.add(db_team)
|
||||
|
@ -50,7 +50,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -79,7 +79,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -48,7 +48,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -77,7 +77,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
@ -50,7 +50,7 @@ def on_startup():
|
||||
@app.post("/heroes/", response_model=HeroRead)
|
||||
def create_hero(hero: HeroCreate):
|
||||
with Session(engine) as session:
|
||||
db_hero = Hero.from_orm(hero)
|
||||
db_hero = Hero.model_validate(hero)
|
||||
session.add(db_hero)
|
||||
session.commit()
|
||||
session.refresh(db_hero)
|
||||
@ -79,7 +79,7 @@ def update_hero(hero_id: int, hero: HeroUpdate):
|
||||
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)
|
||||
hero_data = hero.model_dump(exclude_unset=True)
|
||||
for key, value in hero_data.items():
|
||||
setattr(db_hero, key, value)
|
||||
session.add(db_hero)
|
||||
|
Reference in New Issue
Block a user