【发布时间】:2021-06-30 02:58:22
【问题描述】:
我想创建一个带有构造函数的 Pydantic 类,该构造函数对输入进行一些数学运算并相应地设置对象变量:
class PleaseCoorperate(BaseModel):
self0: str
next0: str
def __init__(self, page: int, total: int, size: int):
# Do some math here and later set the values
self.self0 = ""
self.next0 = ""
但是当我尝试实际使用该类 page = PleaseCoorperate(0, 1, 50) 时,我得到了这个错误:
main_test.py:16:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
main.py:46: in __init__
self.self0 = ""
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
> ???
E AttributeError: __fields_set__
这里发生了什么?我不能在 Pydantic 类上使用构造函数吗?
【问题讨论】: