Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb584faf

Browse files
authored
📝 Add notes about Pydantic v2's new.model_dump() (#10929)
1 parent1334485 commitb584faf

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

‎docs/en/docs/how-to/async-sql-encode-databases.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ Create the *path operation function* to create notes:
114114
{!../../../docs_src/async_sql_databases/tutorial001.py!}
115115
```
116116

117+
!!! info
118+
In Pydantic v1 the method was called`.dict()`, it was deprecated (but still supported) in Pydantic v2, and renamed to`.model_dump()`.
119+
120+
The examples here use `.dict()` for compatibility with Pydantic v1, but you should use `.model_dump()` instead if you can use Pydantic v2.
121+
117122
!!! Note
118123
Notice that as we communicate with the database using`await`, the*path operation function* is declared with`async`.
119124

‎docs/en/docs/tutorial/body-updates.md‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,14 @@ This means that you can send only the data that you want to update, leaving the
5959

6060
###Using Pydantic's`exclude_unset` parameter
6161

62-
If you want to receive partial updates, it's very useful to use the parameter`exclude_unset` in Pydantic's model's`.dict()`.
62+
If you want to receive partial updates, it's very useful to use the parameter`exclude_unset` in Pydantic's model's`.model_dump()`.
6363

64-
Like`item.dict(exclude_unset=True)`.
64+
Like`item.model_dump(exclude_unset=True)`.
65+
66+
!!! info
67+
In Pydantic v1 the method was called`.dict()`, it was deprecated (but still supported) in Pydantic v2, and renamed to`.model_dump()`.
68+
69+
The examples here use `.dict()` for compatibility with Pydantic v1, but you should use `.model_dump()` instead if you can use Pydantic v2.
6570

6671
That would generate a`dict` with only the data that was set when creating the`item` model, excluding default values.
6772

@@ -87,9 +92,14 @@ Then you can use this to generate a `dict` with only the data that was set (sent
8792

8893
###Using Pydantic's`update` parameter
8994

90-
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.
95+
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.
96+
97+
!!! info
98+
In Pydantic v1 the method was called`.copy()`, it was deprecated (but still supported) in Pydantic v2, and renamed to`.model_copy()`.
99+
100+
The examples here use `.copy()` for compatibility with Pydantic v1, but you should use `.model_copy()` instead if you can use Pydantic v2.
91101

92-
Like`stored_item_model.copy(update=update_data)`:
102+
Like`stored_item_model.model_copy(update=update_data)`:
93103

94104
=== "Python 3.10+"
95105

@@ -120,7 +130,7 @@ In summary, to apply partial updates you would:
120130
* 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.
121131
* Create a copy of the stored model, updating it's attributes with the received partial updates (using the`update` parameter).
122132
* Convert the copied model to something that can be stored in your DB (for example, using the`jsonable_encoder`).
123-
* 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`.
133+
* 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`.
124134
* Save the data to your DB.
125135
* Return the updated model.
126136

‎docs/en/docs/tutorial/extra-models.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ Here's a general idea of how the models could look like with their password fiel
2929
{!> ../../../docs_src/extra_models/tutorial001.py!}
3030
```
3131

32+
!!! info
33+
In Pydantic v1 the method was called`.dict()`, it was deprecated (but still supported) in Pydantic v2, and renamed to`.model_dump()`.
34+
35+
The examples here use `.dict()` for compatibility with Pydantic v1, but you should use `.model_dump()` instead if you can use Pydantic v2.
36+
3237
###About`**user_in.dict()`
3338

3439
####Pydantic's`.dict()`

‎docs/en/docs/tutorial/response-model.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ So, if you send a request to that *path operation* for the item with ID `foo`, t
377377
}
378378
```
379379

380+
!!! info
381+
In Pydantic v1 the method was called`.dict()`, it was deprecated (but still supported) in Pydantic v2, and renamed to`.model_dump()`.
382+
383+
The examples here use `.dict()` for compatibility with Pydantic v1, but you should use `.model_dump()` instead if you can use Pydantic v2.
384+
380385
!!! info
381386
FastAPI uses Pydantic model's`.dict()` with <ahref="https://pydantic-docs.helpmanual.io/usage/exporting_models/#modeldict"class="external-link"target="_blank">its`exclude_unset` parameter</a> to achieve this.
382387

‎docs/en/docs/tutorial/sql-databases.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,11 @@ The steps are:
451451
{!../../../docs_src/sql_databases/sql_app/crud.py!}
452452
```
453453

454+
!!! info
455+
In Pydantic v1 the method was called`.dict()`, it was deprecated (but still supported) in Pydantic v2, and renamed to`.model_dump()`.
456+
457+
The examples here use `.dict()` for compatibility with Pydantic v1, but you should use `.model_dump()` instead if you can use Pydantic v2.
458+
454459
!!! tip
455460
The SQLAlchemy model for`User` contains a`hashed_password` that should contain a secure hashed version of the password.
456461

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp