8 changed files with 17 additions and 155 deletions
@ -22,22 +22,13 @@ Here's a general idea of how the models could look like with their password fiel |
|||||
|
|
||||
{* ../../docs_src/extra_models/tutorial001_py310.py hl[7,9,14,20,22,27:28,31:33,38:39] *} |
{* ../../docs_src/extra_models/tutorial001_py310.py hl[7,9,14,20,22,27:28,31:33,38:39] *} |
||||
|
|
||||
|
### About `**user_in.model_dump()` { #about-user-in-dict } |
||||
|
|
||||
/// info |
#### Pydantic's `.model_dump()` { #pydantics-dict } |
||||
|
|
||||
In Pydantic v1 the method was called `.dict()`, it was deprecated (but still supported) in Pydantic v2, and renamed to `.model_dump()`. |
|
||||
|
|
||||
The examples here use `.dict()` for compatibility with Pydantic v1, but you should use `.model_dump()` instead if you can use Pydantic v2. |
|
||||
|
|
||||
/// |
|
||||
|
|
||||
### About `**user_in.dict()` { #about-user-in-dict } |
|
||||
|
|
||||
#### Pydantic's `.dict()` { #pydantics-dict } |
|
||||
|
|
||||
`user_in` is a Pydantic model of class `UserIn`. |
`user_in` is a Pydantic model of class `UserIn`. |
||||
|
|
||||
Pydantic models have a `.dict()` method that returns a `dict` with the model's data. |
Pydantic models have a `.model_dump()` method that returns a `dict` with the model's data. |
||||
|
|
||||
So, if we create a Pydantic object `user_in` like: |
So, if we create a Pydantic object `user_in` like: |
||||
|
|
||||
@ -48,7 +39,7 @@ user_in = UserIn(username="john", password="secret", email="[email protected] |
|||||
and then we call: |
and then we call: |
||||
|
|
||||
```Python |
```Python |
||||
user_dict = user_in.dict() |
user_dict = user_in.model_dump() |
||||
``` |
``` |
||||
|
|
||||
we now have a `dict` with the data in the variable `user_dict` (it's a `dict` instead of a Pydantic model object). |
we now have a `dict` with the data in the variable `user_dict` (it's a `dict` instead of a Pydantic model object). |
||||
@ -104,20 +95,20 @@ UserInDB( |
|||||
|
|
||||
#### A Pydantic model from the contents of another { #a-pydantic-model-from-the-contents-of-another } |
#### A Pydantic model from the contents of another { #a-pydantic-model-from-the-contents-of-another } |
||||
|
|
||||
As in the example above we got `user_dict` from `user_in.dict()`, this code: |
As in the example above we got `user_dict` from `user_in.model_dump()`, this code: |
||||
|
|
||||
```Python |
```Python |
||||
user_dict = user_in.dict() |
user_dict = user_in.model_dump() |
||||
UserInDB(**user_dict) |
UserInDB(**user_dict) |
||||
``` |
``` |
||||
|
|
||||
would be equivalent to: |
would be equivalent to: |
||||
|
|
||||
```Python |
```Python |
||||
UserInDB(**user_in.dict()) |
UserInDB(**user_in.model_dump()) |
||||
``` |
``` |
||||
|
|
||||
...because `user_in.dict()` is a `dict`, and then we make Python "unpack" it by passing it to `UserInDB` prefixed with `**`. |
...because `user_in.model_dump()` is a `dict`, and then we make Python "unpack" it by passing it to `UserInDB` prefixed with `**`. |
||||
|
|
||||
So, we get a Pydantic model from the data in another Pydantic model. |
So, we get a Pydantic model from the data in another Pydantic model. |
||||
|
|
||||
@ -126,7 +117,7 @@ So, we get a Pydantic model from the data in another Pydantic model. |
|||||
And then adding the extra keyword argument `hashed_password=hashed_password`, like in: |
And then adding the extra keyword argument `hashed_password=hashed_password`, like in: |
||||
|
|
||||
```Python |
```Python |
||||
UserInDB(**user_in.dict(), hashed_password=hashed_password) |
UserInDB(**user_in.model_dump(), hashed_password=hashed_password) |
||||
``` |
``` |
||||
|
|
||||
...ends up being like: |
...ends up being like: |
||||
@ -181,7 +172,6 @@ When defining a <a href="https://docs.pydantic.dev/latest/concepts/types/#unions |
|||||
|
|
||||
{* ../../docs_src/extra_models/tutorial003_py310.py hl[1,14:15,18:20,33] *} |
{* ../../docs_src/extra_models/tutorial003_py310.py hl[1,14:15,18:20,33] *} |
||||
|
|
||||
|
|
||||
### `Union` in Python 3.10 { #union-in-python-3-10 } |
### `Union` in Python 3.10 { #union-in-python-3-10 } |
||||
|
|
||||
In this example we pass `Union[PlaneItem, CarItem]` as the value of the argument `response_model`. |
In this example we pass `Union[PlaneItem, CarItem]` as the value of the argument `response_model`. |
||||
@ -204,7 +194,6 @@ For that, use the standard Python `typing.List` (or just `list` in Python 3.9 an |
|||||
|
|
||||
{* ../../docs_src/extra_models/tutorial004_py39.py hl[18] *} |
{* ../../docs_src/extra_models/tutorial004_py39.py hl[18] *} |
||||
|
|
||||
|
|
||||
## Response with arbitrary `dict` { #response-with-arbitrary-dict } |
## Response with arbitrary `dict` { #response-with-arbitrary-dict } |
||||
|
|
||||
You can also declare a response using a plain arbitrary `dict`, declaring just the type of the keys and values, without using a Pydantic model. |
You can also declare a response using a plain arbitrary `dict`, declaring just the type of the keys and values, without using a Pydantic model. |
||||
@ -215,7 +204,6 @@ In this case, you can use `typing.Dict` (or just `dict` in Python 3.9 and above) |
|||||
|
|
||||
{* ../../docs_src/extra_models/tutorial005_py39.py hl[6] *} |
{* ../../docs_src/extra_models/tutorial005_py39.py hl[6] *} |
||||
|
|
||||
|
|
||||
## Recap { #recap } |
## Recap { #recap } |
||||
|
|
||||
Use multiple Pydantic models and inherit freely for each case. |
Use multiple Pydantic models and inherit freely for each case. |
||||
|
|||||
Loading…
Reference in new issue