'How to set BaseModel Field with the name "class"?
I have the following JSON content:
"Device": {
"description": "A Device is an instance of a device class and represents a physical device on an ODN.",
"type": "object",
"required": [
"address",
"name",
"class"
],
"properties": {
"address": {
"description": "The TALQ address of device.",
"type": "string",
"format": "uuid"
},
"name": {
"description": "The name of device.",
"type": "string"
},
"class": {
"description": "The device class name.",
"type": "string"
},
"functions": {
"description": "The individual function models containing the data relating to this device. Each individual function must have a corresponding FunctionDesc at the DeviceClass referred by the 'class' property.",
"type": "array",
"items": {
"$ref": "#/components/schemas/Function"
}
}
}
}
The generated pydantic data is the following:
class Device(BaseModel):
address: UUID = Field(..., description='The TALQ address of device.')
name: str = Field(..., description='The name of device.')
class_: str = Field(..., alias='class', description='The device class name.')
functions: Optional[List[Function1]] = Field(
None,
description="The individual function models containing the data relating to this device. Each individual function must have a corresponding FunctionDesc at the DeviceClass referred by the 'class' property.",
)
As you can see, it changed class to class_ to avoid the conflict with the language class keyword.
But, when I try to construct the object Device I got error:
pydantic.error_wrappers.ValidationError: 1 validation error for Device
class
field required (type=value_error.missing)
Here is how I construct the object:
Device(
address=uuid.UUID(int=0),
name=DEVICE_NAME,
class_=DEVICE_CLASS_NAME,
functions=get_function1_list()
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
