Wednesday 13 October 2021

Pydantic: Create a model from TypedDict

'create_model_from_typeddict' method is used to create a model from TypedDict.

 

create_model_from_typeddict_1.py

from typing_extensions import TypedDict

from pydantic import ValidationError, create_model_from_typeddict

class Employee(TypedDict):
    name: str
    id: int

EmployeeM = create_model_from_typeddict(Employee)

emp1 = EmployeeM(id=1, name='Krishna')
print(emp1.json())

 

Output

{"name": "Krishna", "id": 1}

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment