I have a pydantic model. One of its fields must be supplied by user, however, the second one can be present but it is totally okay if it is missing.
In case of missing age
, I don't want it to be present on pydantic model instance at all.
My Model:
from pydantic import BaseModel
class Employee(BaseModel):
name: str
age: Optional[int]
Problem:
e = Employee(name="Harwey Smith")
e.age # Has age parameter equal to None, even if not provided.
How do I setup my Employee pydantic model not to have age
attribute at all if it is not provided on init? If I would try to access e.nonexistent
attribute, AttributeError
would be thrown. This is what I want to happen for e.age
, if not provided.