'Can I make a default value in pydantic if None is passed in the field without using validators?
Can I make a default value in pydantic if None is passed in the field without using validators?
I have the following code, but it seems to me that the validator here is superfluous for contract_ndfl. Is there any way to do without a validator?
My code:
class User(BaseModel):
user: int
s_name: str
contract_ndfl: Optional[int]
@validator('contract_ndfl')
def set_contract_ndfl(cls, v):
return v or 13
Wishful code:
class User(BaseModel):
user: int
s_name: str
contract_ndfl: Optional[int] = 13
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
