Pydantic: How to pass the default value to a variable if None was passed?
You need to enable validate_assignment
option in model config:
from typing import Optional
from pydantic import BaseModel, validator
class User(BaseModel):
name: Optional[str] = ''
password: Optional[str] = ''
class Config:
validate_assignment = True
@validator('name')
def set_name(cls, name):
return name or 'foo'
user = User(name=None, password='some_password', )
print("Name is ", user.name)
user.name = None
print("Name is ", user.name)
Name is foo
Name is foo
This question asked perfectly so i wanted to provide a wider example, because there are many ways to assign a value dynamically.
Alex's answer is correct but it only works on when the Field directly inherits a dataclass more specifically something like this won't work.
class User(BaseModel):
name: Optional[str] = ""
password: Optional[str] = ""
class Config:
validate_assignment = True
@validator("name")
def set_name(cls, name):
return name or "bar"
user_dict = {"password": "so_secret"}
user_one = User(**user_dict)
Out: name='' password='so_secret'
Validate Always
For performance reasons, by default validators are not called for fields when a value is not supplied. But situations like this when you need to set a Dynamic Default Value we can set that to True
class User(BaseModel):
name: Optional[str] = ""
@validator("name", pre=True, always=True)
def set_name(cls, name):
return name or "bar"
In: user_one = User(name=None)
In: user_two = User()
Out: name='bar'
Out: name='bar'
But there is a one important catch with always, since we are using always=True pydantic would try to validate the default None which would cause an error.
Setting Pre to True
it will call that field before validation error occurs, the default of a validator pre is set to False
, in which case they're called after field validation.
Using Config
But this has some disadvantages.
class User(BaseModel):
name: Optional[str] = ""
class Config:
validate_assignment = True
@validator("name")
def set_name(cls, name):
return name or "foo"
In: user = User(name=None)
Out: name='foo'
When you set it to None it returns the dynamic value correctly but some situations like it is completely None
, it fails.
In: user = User()
Out: name=''
Again you need to set, to make that work.
pre=True
always=True
Using default_factory
This is mostly useful in cases when you want to set a default value, like UUID or datetime etc. In that cases you might want to use default_factory
, but there is a big catch you can't assign a Callable
argument to the default_factory.
class User(BaseModel):
created_at: datetime = Field(default_factory=datetime.now)
In: user = User()
Out: created_at=datetime.datetime(2020, 8, 29, 2, 40, 12, 780986)
Many ways to assign a default value
Method #1: A required id
field with default value
class User(BaseModel):
id: str = uuid.uuid4()
Method #2 An optional id
field with default value
class User(BaseModel):
id: Optional[str] = uuid.uuid4()
Method #3: A required id
field with default value
class User(BaseModel):
id: str = Field(default=uuid.uuid4())
Method #4: A required id
field with default value from callable. This is useful for generating on-demand values such as unique UUIDs
or Timestamps
.
See @yagiz-degirmenci answer.
class User(BaseModel):
id: str = Field(default_factory=uuid.uuid4) # uuid.uuid4 is not executed immediately