Support dual input naming
A model must accept payloads that use either internal Python field names or external aliases.
Pydantic · Pipe
Enables aliased fields to accept input by their Python attribute names as well as their aliases. It defaults to false and is not recommended from Pydantic v2.11 onward because it will be deprecated in v3.
When enabled, an aliased field accepts input under both its Python attribute name and its configured alias. The setting defaults to false, and current documentation advises replacing it with validate_by_name together with validate_by_alias because populate_by_name is deprecated for Pydantic v3.
ConfigDict(populate_by_name=True)| Name | Type | Description |
|---|---|---|
populate_by_name | bool | Whether aliased fields may also be populated using their model attribute names. |
A model configuration setting; it does not return a value.
from pydantic import BaseModel, ConfigDict, Field
class Model(BaseModel):
model_config = ConfigDict(populate_by_name=True)
my_field: str = Field(alias='my_alias')
print(Model(my_field='value'))
print(Model(my_alias='value'))Both the field name and its alias can be used when creating the model.
A model must accept payloads that use either internal Python field names or external aliases.
A migration temporarily needs the older v2.0-v2.10 population behavior.
It allows an aliased field to be populated using either the model attribute name or its alias. The default is false, so only the configured behavior is available unless the setting is enabled.
Follow-up: What should replace it in newer Pydantic code? Use validate_by_name=True and validate_by_alias=True in ConfigDict.
The setting defaults to false. With it enabled, a field declared with an alias can receive input using its Python name as well as that alias.
Follow-up: Why might enabling both validation options be necessary? It preserves acceptance of both naming forms while avoiding the deprecated setting.
The current documentation says its use is not recommended from v2.11 onward and that it will be deprecated in v3. The recommended replacement is separate validation controls for names and aliases.
Follow-up: Can both replacement options be disabled? No; the documentation states that disabling both would make it impossible to populate an attribute.
ConfigDictFieldvalidate_by_namevalidate_by_alias