Using Optional, we can specify a field with optional values.
optional_fields_1.py
from pydantic import BaseModel, ValidationError
from typing import Optional
class Employee(BaseModel):
id: int
name: str
age: Optional[int]
try:
emp1 = Employee(id = 1, name = 'Krishna')
print(emp1)
except ValidationError as e:
print(e.json() + '\n\n')
Output
id=1 name='Krishna' age=None
Previous Next Home
No comments:
Post a Comment