Assigning Pydantic Fields not by alias

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)