How can I create a pydantic object, without useing alias names?
from pydantic import BaseModel, Field
class Params(BaseModel):
var_name: int = Field(alias='var_alias')
Params(var_alias=5) # works
Params(var_name=5) # does not work
As of the pydantic 2.0 release, this behaviour has been updated to use model_config populate_by_name
option which is False
by default.
from pydantic import BaseModel, Field, ConfigDict
class Params(BaseModel):
var_name: int = Field(alias='var_alias')
model_config = ConfigDict(
populate_by_name=True,
)
Params(var_alias=5) # works
Params(var_name=5) # works
For pydantic 1.x, you need to use allow_population_by_field_name
model config option.
from pydantic import BaseModel, Field
class Params(BaseModel):
var_name: int = Field(alias='var_alias')
class Config:
allow_population_by_field_name = True
Params(var_alias=5) # works
Params(var_name=5) # works