Skip to content

Commit 743843c

Browse files
authored
feat: support pydantic2.0 (#1427)
* feat: support pydantic2.0 * fix: test * feat: finish pydantic2.0 support * ci: fix style * fix: pydantic warn
1 parent f6b3eb1 commit 743843c

File tree

11 files changed

+1524
-1463
lines changed

11 files changed

+1524
-1463
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ Changelog
1313
Added
1414
^^^^^
1515
- Allow ForeignKeyField(on_delete=NO_ACTION) (#1393)
16+
- Support `pydantic` 2.0. (#1433)
17+
1618
Fixed
1719
^^^^^
1820
- Fix foreign key constraint not generated on MSSQL Server. (#1400)
1921
- Fix testcase error with python3.11 (#1308)
2022

23+
Breaking Changes
24+
^^^^^^^^^^^^^^^^
25+
- Drop support for `pydantic` 1.x.
26+
- Param `config_class` of `pydantic_model_creator` is renamed to `model_config`.
27+
- Attr `config_class` of `PydanticMeta` is renamed to `model_config`.
28+
2129
0.19.3
2230
------
2331
Added

examples/fastapi/main.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# pylint: disable=E0611,E0401
22
from typing import List
33

4-
from fastapi import FastAPI, HTTPException
4+
from fastapi import FastAPI
55
from models import User_Pydantic, UserIn_Pydantic, Users
66
from pydantic import BaseModel
7+
from starlette.exceptions import HTTPException
78

8-
from tortoise.contrib.fastapi import HTTPNotFoundError, register_tortoise
9+
from tortoise.contrib.fastapi import register_tortoise
910

1011
app = FastAPI(title="Tortoise ORM FastAPI example")
1112

@@ -19,28 +20,24 @@ async def get_users():
1920
return await User_Pydantic.from_queryset(Users.all())
2021

2122

22-
@app.post("/users", response_model=User_Pydantic)
23+
@app.post("/users", response_model=User_Pydantic) # type: ignore
2324
async def create_user(user: UserIn_Pydantic):
2425
user_obj = await Users.create(**user.dict(exclude_unset=True))
2526
return await User_Pydantic.from_tortoise_orm(user_obj)
2627

2728

28-
@app.get(
29-
"/user/{user_id}", response_model=User_Pydantic, responses={404: {"model": HTTPNotFoundError}}
30-
)
29+
@app.get("/user/{user_id}", response_model=User_Pydantic) # type: ignore
3130
async def get_user(user_id: int):
3231
return await User_Pydantic.from_queryset_single(Users.get(id=user_id))
3332

3433

35-
@app.put(
36-
"/user/{user_id}", response_model=User_Pydantic, responses={404: {"model": HTTPNotFoundError}}
37-
)
34+
@app.put("/user/{user_id}", response_model=User_Pydantic) # type: ignore
3835
async def update_user(user_id: int, user: UserIn_Pydantic):
3936
await Users.filter(id=user_id).update(**user.dict(exclude_unset=True))
4037
return await User_Pydantic.from_queryset_single(Users.get(id=user_id))
4138

4239

43-
@app.delete("/user/{user_id}", response_model=Status, responses={404: {"model": HTTPNotFoundError}})
40+
@app.delete("/user/{user_id}", response_model=Status) # type: ignore
4441
async def delete_user(user_id: int):
4542
deleted_count = await Users.filter(id=user_id).delete()
4643
if not deleted_count:

examples/pydantic/basic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ async def run():
8787
await event3.participants.add(team1, team3)
8888

8989
p = await Event_Pydantic.from_tortoise_orm(await Event.get(name="Test"))
90-
print("One Event:", p.json(indent=4))
90+
print("One Event:", p.model_dump_json(indent=4))
9191

9292
p = await Tournament_Pydantic.from_tortoise_orm(await Tournament.get(name="New Tournament"))
93-
print("One Tournament:", p.json(indent=4))
93+
print("One Tournament:", p.model_dump_json(indent=4))
9494

9595
p = await Team_Pydantic.from_tortoise_orm(await Team.get(name="Onesies"))
96-
print("One Team:", p.json(indent=4))
96+
print("One Team:", p.model_dump_json(indent=4))
9797

9898
pl = await Event_Pydantic_List.from_queryset(Event.filter(address__event_id__isnull=True))
99-
print("All Events without addresses:", pl.json(indent=4))
99+
print("All Events without addresses:", pl.model_dump_json(indent=4))
100100

101101

102102
if __name__ == "__main__":

poetry.lock

Lines changed: 640 additions & 686 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ sanic = "*"
7373
# Sample integration - Starlette
7474
starlette = "*"
7575
# Pydantic support
76-
pydantic = "*"
76+
pydantic = "^2.0"
7777
# FastAPI support
7878
fastapi = "*"
7979
asgi_lifespan = "*"

0 commit comments

Comments
 (0)