Skip to content

Commit

Permalink
docs: update pydantic docs accordingly Pydantic V2 (#1469)
Browse files Browse the repository at this point in the history
* docs: update pydantic `.json` and `.dict` methods in docs

* docs: update examples

* docs: update `__root__` elements by `root`

* docs: update `__root__` elements by `root`
  • Loading branch information
plusiv authored Aug 30, 2023
1 parent 0cb82bb commit c030c9c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
24 changes: 12 additions & 12 deletions docs/contrib/pydantic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Here we introduce:
* Creating a Pydantic model from a Tortoise model
* Docstrings & doc-comments are used
* Evaluating the generated schema
* Simple serialisation with both ``.dict()`` and ``.json()``
* Simple serialisation with both ``.model_dump()`` and ``.model_dump_json()``

Source to example: :ref:`example_pydantic_tut1`

Expand Down Expand Up @@ -92,17 +92,17 @@ To serialise an object it is simply *(in an async context)*:
tournament = await Tournament.create(name="New Tournament")
tourpy = await Tournament_Pydantic.from_tortoise_orm(tournament)
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()``
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()``

.. code-block:: py3
>>> print(tourpy.dict())
>>> print(tourpy.model_dump())
{
'id': 1,
'name': 'New Tournament',
'created_at': datetime.datetime(2020, 3, 1, 20, 28, 9, 346808)
}
>>> print(tourpy.json())
>>> print(tourpy.model_dump_json())
{
"id": 1,
"name": "New Tournament",
Expand Down Expand Up @@ -202,13 +202,13 @@ To serialise an object it is simply *(in an async context)*:
tourpy = await Tournament_Pydantic_List.from_queryset(Tournament.all())
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()``
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()``

.. code-block:: py3
>>> print(tourpy.dict())
>>> print(tourpy.model_dump())
{
'__root__': [
'root': [
{
'id': 2,
'name': 'Another',
Expand All @@ -226,7 +226,7 @@ And one could get the contents by using `regular Pydantic-object methods <https:
}
]
}
>>> print(tourpy.json())
>>> print(tourpy.model_dump_json())
[
{
"id": 2,
Expand All @@ -245,7 +245,7 @@ And one could get the contents by using `regular Pydantic-object methods <https:
}
]
Note how ``.dict()`` has a ``_root__`` element with the list, but the ``.json()`` has the list as root.
Note how ``.model_dump()`` has a ``root`` element with the list, but the ``.model_dump_json()`` has the list as root.
Also note how the results are sorted alphabetically by ``name``.


Expand Down Expand Up @@ -479,7 +479,7 @@ Lets create and serialise the objects and see what they look like *(in an async
# Serialise Tournament
tourpy = await Tournament_Pydantic.from_tortoise_orm(tournament)
>>> print(tourpy.json())
>>> print(tourpy.model_dump_json())
{
"id": 1,
"name": "New Tournament",
Expand All @@ -499,7 +499,7 @@ And serialising the event *(in an async context)*:
eventpy = await Event_Pydantic.from_tortoise_orm(event)
>>> print(eventpy.json())
>>> print(eventpy.model_dump_json())
{
"id": 1,
"name": "The Event",
Expand Down Expand Up @@ -675,7 +675,7 @@ Lets create and serialise the objects and see what they look like *(in an async
# Serialise Tournament
tourpy = await Tournament_Pydantic.from_tortoise_orm(tournament)
>>> print(tourpy.json())
>>> print(tourpy.model_dump_json())
{
"id": 1,
"name": "New Tournament",
Expand Down
2 changes: 1 addition & 1 deletion examples/blacksheep/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def users_patch(id: UUID, user: UserPydanticIn) -> UserPydanticOut:

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


Expand Down
4 changes: 2 additions & 2 deletions examples/pydantic/tutorial_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Creating a Pydantic model from a Tortoise model
* Docstrings & doc-comments are used
* Evaluating the generated schema
* Simple serialisation with both .dict() and .json()
* Simple serialisation with both .model_dump() and .model_dump_json()
"""
from tortoise import Tortoise, fields, run_async
from tortoise.contrib.pydantic import pydantic_model_creator
Expand Down Expand Up @@ -38,7 +38,7 @@ async def run():
tourpy = await Tournament_Pydantic.from_tortoise_orm(tournament)

# As Python dict with Python objects (e.g. datetime)
print(tourpy.dict())
print(tourpy.model_dump())
# As serialised JSON (e.g. datetime is ISO8601 string representation)
print(tourpy.json(indent=4))

Expand Down
4 changes: 2 additions & 2 deletions examples/pydantic/tutorial_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ async def run():
tourpy = await Tournament_Pydantic_List.from_queryset(Tournament.all())

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

Expand Down

0 comments on commit c030c9c

Please sign in to comment.