Skip to content

Commit 684ff65

Browse files
Upgrade to Pydantic 2
Change imports Undefined => PydanticUndefined Update SQLModelMetaclass and SQLModel __init__ and __new__ functions Update SQL Alchemy type inference
1 parent 4ce8d07 commit 684ff65

File tree

17 files changed

+175
-283
lines changed

17 files changed

+175
-283
lines changed

docs/tutorial/fastapi/multiple-models.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ Now we use the type annotation `HeroCreate` for the request JSON data in the `he
174174
# Code below omitted 👇
175175
```
176176

177-
Then we create a new `Hero` (this is the actual **table** model that saves things to the database) using `Hero.from_orm()`.
177+
Then we create a new `Hero` (this is the actual **table** model that saves things to the database) using `Hero.model_validate()`.
178178

179-
The method `.from_orm()` reads data from another object with attributes and creates a new instance of this class, in this case `Hero`.
179+
The method `.model_validate()` reads data from another object with attributes and creates a new instance of this class, in this case `Hero`.
180180

181181
The alternative is `Hero.parse_obj()` that reads data from a dictionary.
182182

183-
But as in this case, we have a `HeroCreate` instance in the `hero` variable. This is an object with attributes, so we use `.from_orm()` to read those attributes.
183+
But as in this case, we have a `HeroCreate` instance in the `hero` variable. This is an object with attributes, so we use `.model_validate()` to read those attributes.
184184

185185
With this, we create a new `Hero` instance (the one for the database) and put it in the variable `db_hero` from the data in the `hero` variable that is the `HeroCreate` instance we received from the request.
186186

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def on_startup():
5454

5555
@app.post("/heroes/", response_model=HeroRead)
5656
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
57-
db_hero = Hero.from_orm(hero)
57+
db_hero = Hero.model_validate(hero)
5858
session.add(db_hero)
5959
session.commit()
6060
session.refresh(db_hero)

docs_src/tutorial/fastapi/delete/tutorial001.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def on_startup():
5050
@app.post("/heroes/", response_model=HeroRead)
5151
def create_hero(hero: HeroCreate):
5252
with Session(engine) as session:
53-
db_hero = Hero.from_orm(hero)
53+
db_hero = Hero.model_validate(hero)
5454
session.add(db_hero)
5555
session.commit()
5656
session.refresh(db_hero)

docs_src/tutorial/fastapi/limit_and_offset/tutorial001.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def on_startup():
4444
@app.post("/heroes/", response_model=HeroRead)
4545
def create_hero(hero: HeroCreate):
4646
with Session(engine) as session:
47-
db_hero = Hero.from_orm(hero)
47+
db_hero = Hero.model_validate(hero)
4848
session.add(db_hero)
4949
session.commit()
5050
session.refresh(db_hero)

docs_src/tutorial/fastapi/multiple_models/tutorial001.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def on_startup():
4646
@app.post("/heroes/", response_model=HeroRead)
4747
def create_hero(hero: HeroCreate):
4848
with Session(engine) as session:
49-
db_hero = Hero.from_orm(hero)
49+
db_hero = Hero.model_validate(hero)
5050
session.add(db_hero)
5151
session.commit()
5252
session.refresh(db_hero)

docs_src/tutorial/fastapi/multiple_models/tutorial002.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def on_startup():
4444
@app.post("/heroes/", response_model=HeroRead)
4545
def create_hero(hero: HeroCreate):
4646
with Session(engine) as session:
47-
db_hero = Hero.from_orm(hero)
47+
db_hero = Hero.model_validate(hero)
4848
session.add(db_hero)
4949
session.commit()
5050
session.refresh(db_hero)

docs_src/tutorial/fastapi/read_one/tutorial001.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def on_startup():
4444
@app.post("/heroes/", response_model=HeroRead)
4545
def create_hero(hero: HeroCreate):
4646
with Session(engine) as session:
47-
db_hero = Hero.from_orm(hero)
47+
db_hero = Hero.model_validate(hero)
4848
session.add(db_hero)
4949
session.commit()
5050
session.refresh(db_hero)

docs_src/tutorial/fastapi/relationships/tutorial001.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def on_startup():
9292

9393
@app.post("/heroes/", response_model=HeroRead)
9494
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
95-
db_hero = Hero.from_orm(hero)
95+
db_hero = Hero.model_validate(hero)
9696
session.add(db_hero)
9797
session.commit()
9898
session.refresh(db_hero)
@@ -147,7 +147,7 @@ def delete_hero(*, session: Session = Depends(get_session), hero_id: int):
147147

148148
@app.post("/teams/", response_model=TeamRead)
149149
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
150-
db_team = Team.from_orm(team)
150+
db_team = Team.model_validate(team)
151151
session.add(db_team)
152152
session.commit()
153153
session.refresh(db_team)

docs_src/tutorial/fastapi/session_with_dependency/tutorial001.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def on_startup():
5454

5555
@app.post("/heroes/", response_model=HeroRead)
5656
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
57-
db_hero = Hero.from_orm(hero)
57+
db_hero = Hero.model_validate(hero)
5858
session.add(db_hero)
5959
session.commit()
6060
session.refresh(db_hero)

docs_src/tutorial/fastapi/teams/tutorial001.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def on_startup():
8383

8484
@app.post("/heroes/", response_model=HeroRead)
8585
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
86-
db_hero = Hero.from_orm(hero)
86+
db_hero = Hero.model_validate(hero)
8787
session.add(db_hero)
8888
session.commit()
8989
session.refresh(db_hero)
@@ -138,7 +138,7 @@ def delete_hero(*, session: Session = Depends(get_session), hero_id: int):
138138

139139
@app.post("/teams/", response_model=TeamRead)
140140
def create_team(*, session: Session = Depends(get_session), team: TeamCreate):
141-
db_team = Team.from_orm(team)
141+
db_team = Team.model_validate(team)
142142
session.add(db_team)
143143
session.commit()
144144
session.refresh(db_team)

0 commit comments

Comments
 (0)