Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support pydantic2.0 #1427

Merged
merged 5 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ Changelog
Added
^^^^^
- Allow ForeignKeyField(on_delete=NO_ACTION) (#1393)
- Support `pydantic` 2.0. (#1433)

Fixed
^^^^^
- Fix foreign key constraint not generated on MSSQL Server. (#1400)
- Fix testcase error with python3.11 (#1308)

Breaking Changes
^^^^^^^^^^^^^^^^
- Drop support for `pydantic` 1.x.
- Param `config_class` of `pydantic_model_creator` is renamed to `model_config`.
- Attr `config_class` of `PydanticMeta` is renamed to `model_config`.

0.19.3
------
Added
Expand Down
17 changes: 7 additions & 10 deletions examples/fastapi/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# pylint: disable=E0611,E0401
from typing import List

from fastapi import FastAPI, HTTPException
from fastapi import FastAPI
from models import User_Pydantic, UserIn_Pydantic, Users
from pydantic import BaseModel
from starlette.exceptions import HTTPException

from tortoise.contrib.fastapi import HTTPNotFoundError, register_tortoise
from tortoise.contrib.fastapi import register_tortoise

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

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


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


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


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


@app.delete("/user/{user_id}", response_model=Status, responses={404: {"model": HTTPNotFoundError}})
@app.delete("/user/{user_id}", response_model=Status) # type: ignore
async def delete_user(user_id: int):
deleted_count = await Users.filter(id=user_id).delete()
if not deleted_count:
Expand Down
8 changes: 4 additions & 4 deletions examples/pydantic/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ async def run():
await event3.participants.add(team1, team3)

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

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

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

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


if __name__ == "__main__":
Expand Down
1,326 changes: 640 additions & 686 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ sanic = "*"
# Sample integration - Starlette
starlette = "*"
# Pydantic support
pydantic = "*"
pydantic = "^2.0"
# FastAPI support
fastapi = "*"
asgi_lifespan = "*"
Expand Down
Loading
Loading