mirror of https://github.com/tiangolo/fastapi.git
📝 Add notes about Pydantic v2's new `.model_dump()` (#10929)
This commit is contained in:
parent
1334485435
commit
b584faffee
|
|
@ -114,6 +114,11 @@ Create the *path operation function* to create notes:
|
||||||
{!../../../docs_src/async_sql_databases/tutorial001.py!}
|
{!../../../docs_src/async_sql_databases/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
!!! info
|
||||||
|
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.
|
||||||
|
|
||||||
!!! Note
|
!!! Note
|
||||||
Notice that as we communicate with the database using `await`, the *path operation function* is declared with `async`.
|
Notice that as we communicate with the database using `await`, the *path operation function* is declared with `async`.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,9 +59,14 @@ This means that you can send only the data that you want to update, leaving the
|
||||||
|
|
||||||
### Using Pydantic's `exclude_unset` parameter
|
### Using Pydantic's `exclude_unset` parameter
|
||||||
|
|
||||||
If you want to receive partial updates, it's very useful to use the parameter `exclude_unset` in Pydantic's model's `.dict()`.
|
If you want to receive partial updates, it's very useful to use the parameter `exclude_unset` in Pydantic's model's `.model_dump()`.
|
||||||
|
|
||||||
Like `item.dict(exclude_unset=True)`.
|
Like `item.model_dump(exclude_unset=True)`.
|
||||||
|
|
||||||
|
!!! info
|
||||||
|
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.
|
||||||
|
|
||||||
That would generate a `dict` with only the data that was set when creating the `item` model, excluding default values.
|
That would generate a `dict` with only the data that was set when creating the `item` model, excluding default values.
|
||||||
|
|
||||||
|
|
@ -87,9 +92,14 @@ Then you can use this to generate a `dict` with only the data that was set (sent
|
||||||
|
|
||||||
### Using Pydantic's `update` parameter
|
### Using Pydantic's `update` parameter
|
||||||
|
|
||||||
Now, you can create a copy of the existing model using `.copy()`, and pass the `update` parameter with a `dict` containing the data to update.
|
Now, you can create a copy of the existing model using `.model_copy()`, and pass the `update` parameter with a `dict` containing the data to update.
|
||||||
|
|
||||||
Like `stored_item_model.copy(update=update_data)`:
|
!!! info
|
||||||
|
In Pydantic v1 the method was called `.copy()`, it was deprecated (but still supported) in Pydantic v2, and renamed to `.model_copy()`.
|
||||||
|
|
||||||
|
The examples here use `.copy()` for compatibility with Pydantic v1, but you should use `.model_copy()` instead if you can use Pydantic v2.
|
||||||
|
|
||||||
|
Like `stored_item_model.model_copy(update=update_data)`:
|
||||||
|
|
||||||
=== "Python 3.10+"
|
=== "Python 3.10+"
|
||||||
|
|
||||||
|
|
@ -120,7 +130,7 @@ In summary, to apply partial updates you would:
|
||||||
* This way you can update only the values actually set by the user, instead of overriding values already stored with default values in your model.
|
* This way you can update only the values actually set by the user, instead of overriding values already stored with default values in your model.
|
||||||
* Create a copy of the stored model, updating it's attributes with the received partial updates (using the `update` parameter).
|
* Create a copy of the stored model, updating it's attributes with the received partial updates (using the `update` parameter).
|
||||||
* Convert the copied model to something that can be stored in your DB (for example, using the `jsonable_encoder`).
|
* Convert the copied model to something that can be stored in your DB (for example, using the `jsonable_encoder`).
|
||||||
* This is comparable to using the model's `.dict()` method again, but it makes sure (and converts) the values to data types that can be converted to JSON, for example, `datetime` to `str`.
|
* This is comparable to using the model's `.model_dump()` method again, but it makes sure (and converts) the values to data types that can be converted to JSON, for example, `datetime` to `str`.
|
||||||
* Save the data to your DB.
|
* Save the data to your DB.
|
||||||
* Return the updated model.
|
* Return the updated model.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,11 @@ Here's a general idea of how the models could look like with their password fiel
|
||||||
{!> ../../../docs_src/extra_models/tutorial001.py!}
|
{!> ../../../docs_src/extra_models/tutorial001.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
!!! info
|
||||||
|
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()`
|
#### Pydantic's `.dict()`
|
||||||
|
|
|
||||||
|
|
@ -377,6 +377,11 @@ So, if you send a request to that *path operation* for the item with ID `foo`, t
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
!!! info
|
||||||
|
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.
|
||||||
|
|
||||||
!!! info
|
!!! info
|
||||||
FastAPI uses Pydantic model's `.dict()` with <a href="https://pydantic-docs.helpmanual.io/usage/exporting_models/#modeldict" class="external-link" target="_blank">its `exclude_unset` parameter</a> to achieve this.
|
FastAPI uses Pydantic model's `.dict()` with <a href="https://pydantic-docs.helpmanual.io/usage/exporting_models/#modeldict" class="external-link" target="_blank">its `exclude_unset` parameter</a> to achieve this.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -451,6 +451,11 @@ The steps are:
|
||||||
{!../../../docs_src/sql_databases/sql_app/crud.py!}
|
{!../../../docs_src/sql_databases/sql_app/crud.py!}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
!!! info
|
||||||
|
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.
|
||||||
|
|
||||||
!!! tip
|
!!! tip
|
||||||
The SQLAlchemy model for `User` contains a `hashed_password` that should contain a secure hashed version of the password.
|
The SQLAlchemy model for `User` contains a `hashed_password` that should contain a secure hashed version of the password.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue