Skip to content

Commit c030c9c

Browse files
authored
docs: update pydantic docs accordingly Pydantic V2 (#1469)
* docs: update pydantic `.json` and `.dict` methods in docs * docs: update examples * docs: update `__root__` elements by `root` * docs: update `__root__` elements by `root`
1 parent 0cb82bb commit c030c9c

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

docs/contrib/pydantic.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Here we introduce:
2525
* Creating a Pydantic model from a Tortoise model
2626
* Docstrings & doc-comments are used
2727
* Evaluating the generated schema
28-
* Simple serialisation with both ``.dict()`` and ``.json()``
28+
* Simple serialisation with both ``.model_dump()`` and ``.model_dump_json()``
2929

3030
Source to example: :ref:`example_pydantic_tut1`
3131

@@ -92,17 +92,17 @@ To serialise an object it is simply *(in an async context)*:
9292
tournament = await Tournament.create(name="New Tournament")
9393
tourpy = await Tournament_Pydantic.from_tortoise_orm(tournament)
9494
95-
And one could get the contents by using `regular Pydantic-object methods <https://pydantic-docs.helpmanual.io/usage/exporting_models/>`_, such as ``.dict()`` or ``.json()``
95+
And one could get the contents by using `regular Pydantic-object methods <https://pydantic-docs.helpmanual.io/usage/exporting_models/>`_, such as ``.model_dump()`` or ``.model_dump_json()``
9696

9797
.. code-block:: py3
9898
99-
>>> print(tourpy.dict())
99+
>>> print(tourpy.model_dump())
100100
{
101101
'id': 1,
102102
'name': 'New Tournament',
103103
'created_at': datetime.datetime(2020, 3, 1, 20, 28, 9, 346808)
104104
}
105-
>>> print(tourpy.json())
105+
>>> print(tourpy.model_dump_json())
106106
{
107107
"id": 1,
108108
"name": "New Tournament",
@@ -202,13 +202,13 @@ To serialise an object it is simply *(in an async context)*:
202202
203203
tourpy = await Tournament_Pydantic_List.from_queryset(Tournament.all())
204204
205-
And one could get the contents by using `regular Pydantic-object methods <https://pydantic-docs.helpmanual.io/usage/exporting_models/>`_, such as ``.dict()`` or ``.json()``
205+
And one could get the contents by using `regular Pydantic-object methods <https://pydantic-docs.helpmanual.io/usage/exporting_models/>`_, such as ``.model_dump()`` or ``.model_dump_json()``
206206

207207
.. code-block:: py3
208208
209-
>>> print(tourpy.dict())
209+
>>> print(tourpy.model_dump())
210210
{
211-
'__root__': [
211+
'root': [
212212
{
213213
'id': 2,
214214
'name': 'Another',
@@ -226,7 +226,7 @@ And one could get the contents by using `regular Pydantic-object methods <https:
226226
}
227227
]
228228
}
229-
>>> print(tourpy.json())
229+
>>> print(tourpy.model_dump_json())
230230
[
231231
{
232232
"id": 2,
@@ -245,7 +245,7 @@ And one could get the contents by using `regular Pydantic-object methods <https:
245245
}
246246
]
247247
248-
Note how ``.dict()`` has a ``_root__`` element with the list, but the ``.json()`` has the list as root.
248+
Note how ``.model_dump()`` has a ``root`` element with the list, but the ``.model_dump_json()`` has the list as root.
249249
Also note how the results are sorted alphabetically by ``name``.
250250

251251

@@ -479,7 +479,7 @@ Lets create and serialise the objects and see what they look like *(in an async
479479
# Serialise Tournament
480480
tourpy = await Tournament_Pydantic.from_tortoise_orm(tournament)
481481
482-
>>> print(tourpy.json())
482+
>>> print(tourpy.model_dump_json())
483483
{
484484
"id": 1,
485485
"name": "New Tournament",
@@ -499,7 +499,7 @@ And serialising the event *(in an async context)*:
499499
500500
eventpy = await Event_Pydantic.from_tortoise_orm(event)
501501
502-
>>> print(eventpy.json())
502+
>>> print(eventpy.model_dump_json())
503503
{
504504
"id": 1,
505505
"name": "The Event",
@@ -675,7 +675,7 @@ Lets create and serialise the objects and see what they look like *(in an async
675675
# Serialise Tournament
676676
tourpy = await Tournament_Pydantic.from_tortoise_orm(tournament)
677677
678-
>>> print(tourpy.json())
678+
>>> print(tourpy.model_dump_json())
679679
{
680680
"id": 1,
681681
"name": "New Tournament",

examples/blacksheep/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async def users_patch(id: UUID, user: UserPydanticIn) -> UserPydanticOut:
4343

4444
@app.router.put("/{id}")
4545
async def users_put(id: UUID, user: UserPydanticIn) -> UserPydanticOut:
46-
await Users.filter(id=id).update(**user.dict())
46+
await Users.filter(id=id).update(**user.model_dump())
4747
return ok(await UserPydanticOut.from_tortoise_orm(await Users.get(id=id)))
4848

4949

examples/pydantic/tutorial_1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Creating a Pydantic model from a Tortoise model
66
* Docstrings & doc-comments are used
77
* Evaluating the generated schema
8-
* Simple serialisation with both .dict() and .json()
8+
* Simple serialisation with both .model_dump() and .model_dump_json()
99
"""
1010
from tortoise import Tortoise, fields, run_async
1111
from tortoise.contrib.pydantic import pydantic_model_creator
@@ -38,7 +38,7 @@ async def run():
3838
tourpy = await Tournament_Pydantic.from_tortoise_orm(tournament)
3939

4040
# As Python dict with Python objects (e.g. datetime)
41-
print(tourpy.dict())
41+
print(tourpy.model_dump())
4242
# As serialised JSON (e.g. datetime is ISO8601 string representation)
4343
print(tourpy.json(indent=4))
4444

examples/pydantic/tutorial_2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ async def run():
4545
tourpy = await Tournament_Pydantic_List.from_queryset(Tournament.all())
4646

4747
# As Python dict with Python objects (e.g. datetime)
48-
# Note that the root element is '__root__' that contains the root element.
49-
print(tourpy.dict())
48+
# Note that the root element is 'root' that contains the root element.
49+
print(tourpy.model_dump())
5050
# As serialised JSON (e.g. datetime is ISO8601 string representation)
5151
print(tourpy.json(indent=4))
5252

0 commit comments

Comments
 (0)