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
import typing
class Contact(object): pass
class BaseModel(object): pass
class ContactList(BaseModel):
type: str
contacts: typing.List[Contact]
Not sure if it is what you are looking for? typing.List[SomeType]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.