I am currently implementing operations over Intercom data model using Pydantic.
Lots of Intercom models implements list of objects as below:
"contacts":{
"type": "contact.list"
"contacts": [
Contact1,
Contact2,
…
]
}
And we have same logic for some other objects lists.
How can I define a dynamic and generic Pydantic base class to manage it ?
I firstly think to:
class BaseList(BaseModel):
type: str
???
@validator('type')
def check_type(cls, v):
assert v.endswith(".list")
return v
And then usage as below in my parent model:
class ParentModel(BaseModel):
contacts: BaseList[Contact]
But I don’t know how to manage the dynamic field name and it’s nested associated key.
Thank you for your help !
Regards