]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
📝 Add notes about Pydantic v2's new `.model_dump()` (#10929)
authorSebastián Ramírez <tiangolo@gmail.com>
Wed, 10 Jan 2024 19:13:55 +0000 (23:13 +0400)
committerGitHub <noreply@github.com>
Wed, 10 Jan 2024 19:13:55 +0000 (19:13 +0000)
docs/en/docs/how-to/async-sql-encode-databases.md
docs/en/docs/tutorial/body-updates.md
docs/en/docs/tutorial/extra-models.md
docs/en/docs/tutorial/response-model.md
docs/en/docs/tutorial/sql-databases.md

index 697167f79026791cb0fe8f1e4e73e7eb660390ce..0e2ccce78dbc34356012e5536424bbed70f04662 100644 (file)
@@ -114,6 +114,11 @@ Create the *path operation function* to create notes:
 {!../../../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
     Notice that as we communicate with the database using `await`, the *path operation function* is declared with `async`.
 
index 3341f2d5d88b49637433f959e9c596535a496f00..39d133c55f3dd8d813a9683ab911f36aa37e5e53 100644 (file)
@@ -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
 
-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.
 
@@ -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
 
-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.
+
+!!! 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.copy(update=update_data)`:
+Like `stored_item_model.model_copy(update=update_data)`:
 
 === "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.
 * 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`).
-    * 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.
 * Return the updated model.
 
index 590d095bd24578ae8d7373dce152577777434731..d83b6bc8594bbdce99246df4cd76f0bbaddfbf45 100644 (file)
@@ -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!}
     ```
 
+!!! 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()`
 
 #### Pydantic's `.dict()`
index d6d3d61cb4b24bc3a7ec17ac603c0f9faaa2e1f0..d5683ac7f2e8aca3bfbb4aeaf7b4910b4067624d 100644 (file)
@@ -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
     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.
 
index ce6507912252e1e08e62005604934b4a6a584159..1bc87a702d1b8f7bafea7f364d407a4957c16e13 100644 (file)
@@ -451,6 +451,11 @@ The steps are:
 {!../../../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
     The SQLAlchemy model for `User` contains a `hashed_password` that should contain a secure hashed version of the password.