'Pydantic how to create a model with required fields and dynamic fields?
I have a dynamic json like:
{
"ts": 1111111, // this field is required
... // others are dynamic
}
The fields are all dynamic except ts. E.g:
{"ts":111,"f1":"aa","f2":"bb"}
{"ts":222,"f3":"cc","f4":"dd"}
How to declare this model with Pydantic?
class JsonData(BaseModel):
ts: int
... # and then?
Equivalent to typescript:
interface JsonData {
ts: number;
[key: string]: any;
}
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
