You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reorganize extra configuration documentation (#11213)
The section in the concepts page was moved to the top, as
the default behavior is usually confusing for some users.
The section was also simplified to just give some insights,
and we refer to the API docs to avoid duplication. The API
documentation was updated to take examples from both the
existing concepts section and API documentation.
Add more info about support for extra data with dataclasses
Remove unused annotation comment.
Besides, using these abstract types can also lead to [poor validation performance](./performance.md#sequence-vs-list-or-tuple-with-mapping-vs-dict), and in general using concrete container types
222
222
will avoid unnecessary checks.
223
223
224
+
<!-- old anchor added for backwards compatibility -->
225
+
[](){#extra-fields}
226
+
227
+
## Extra data
228
+
229
+
By default, Pydantic models **won't error when you provide extra data**, and these values will simply be ignored:
230
+
231
+
```python
232
+
from pydantic import BaseModel
233
+
234
+
235
+
classModel(BaseModel):
236
+
x: int
237
+
238
+
239
+
m = Model(x=1, y='a')
240
+
assert m.model_dump() == {'x': 1}
241
+
```
242
+
243
+
The [`extra`][pydantic.ConfigDict.extra] configuration value can be used to control this behavior:
244
+
245
+
```python
246
+
from pydantic import BaseModel, ConfigDict
247
+
248
+
249
+
classModel(BaseModel):
250
+
x: int
251
+
252
+
model_config = ConfigDict(extra='allow')
253
+
254
+
255
+
m = Model(x=1, y='a') # (1)!
256
+
assert m.model_dump() == {'x': 1, 'y': 'a'}
257
+
assert m.__pydantic_extra__ == {'y': 'a'}
258
+
```
259
+
260
+
1. If [`extra`][pydantic.ConfigDict.extra] was set to `'forbid'`, this would fail.
261
+
262
+
The configuration can take three values:
263
+
264
+
-`'ignore'`: Providing extra data is ignored (the default).
265
+
-`'forbid'`: Providing extra data is not permitted.
266
+
-`'allow'`: Providing extra data is allowed and stored in the `__pydantic_extra__` dictionary attribute.
267
+
The `__pydantic_extra__` can explicitly be annotated to provide validation for extra fields.
268
+
269
+
For more details, refer to the [`extra`][pydantic.ConfigDict.extra] API documentation.
270
+
271
+
Pydantic dataclasses also support extra data (see the [dataclass configuration](./dataclasses.md#dataclass-config) section).
272
+
224
273
## Nested models
225
274
226
275
More complex hierarchical data structures can be defined using models themselves as types in annotations.
@@ -639,7 +688,7 @@ Here are some additional notes on the behavior of [`model_construct()`][pydantic
639
688
creating the model with validation.
640
689
* No `__init__` method from the model or any of its parent classes will be called, even when a custom `__init__` method is defined.
641
690
642
-
!!! note "On [extra fields](#extra-fields) behavior with [`model_construct()`][pydantic.main.BaseModel.model_construct]"
691
+
!!! note "On [extra data](#extra-data) behavior with [`model_construct()`][pydantic.main.BaseModel.model_construct]"
643
692
* For models with [`extra`][pydantic.ConfigDict.extra] set to `'allow'`, data not corresponding to fields will be correctly stored in
644
693
the `__pydantic_extra__` dictionary and saved to the model's `__dict__` attribute.
645
694
* For models with [`extra`][pydantic.ConfigDict.extra] set to `'ignore'`, data not corresponding to fields will be ignored — that is,
0 commit comments