Friday 24 September 2021

Pydantic: Use models as types

You can use models as types in Pydantic.

 

nested_types_1.py

from pydantic import BaseModel, ValidationError

class Address(BaseModel):
    street: str
    city: str
    country: str

class Employee(BaseModel):
    id: int
    name: str
    age: int
    address: Address

emp1 = Employee(id = 1, name = 'Ptr', age = 23, address = Address(street = 'Chowdeswari', city = "Bangalore", country = 'India'))

emp1Json = emp1.json(exclude = {'address' : {'street', 'country'}})
emp2Json = emp1.json(exclude = {'address' : {'street', 'country'}, 'age': ...})

print('emp1Json -> ' + emp1Json)
print('emp2Json -> ' + emp2Json)

 

Output

emp1Json -> {"id": 1, "name": "Ptr", "age": 23, "address": {"city": "Bangalore"}}
emp2Json -> {"id": 1, "name": "Ptr", "address": {"city": "Bangalore"}}

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment