我需要将WKBElement类型字段转换为坐标列表。转换功能在Pydantic之外可以正常使用。但是,在Pydantic中,它给了我这个错误:
pydantic.error_wrappers.ValidationError: 1 validation error for AddressOut
response -> coordinates
(type=assertion_error)
在Pydantic文档中,这意味着验证程序缺少assert
。我只是不明白我该如何实现。
代码:
def WKBElement_to_point(WKB:geoalchemy2.elements.WKBElement)->List[float]:
return geoalchemy2.shape.to_shape(WKB).coords[0]
class AddressOut(AddressIn):
id: int
coordinates: List[float]
created_at: datetime.datetime = None
updated_at: datetime.datetime = None
disabled: bool
user_id: int
_coordinates = validator('coordinates', allow_reuse=True,whole=True, pre=True, always=True)(WKBElement_to_point)
class Config:
orm_mode = True