I have a pydantic class such as:
from pydantic import BaseModel
class Programmer(BaseModel):
python_skill: float
stackoverflow_skill: float
total_score: float = None
Now I am calculating the total_score according to the other fields:
@validator("total_score", always=True)
def calculat_total_score(cls, v, *, values):
return values.get("python_skill") + values.get("stackoverflow_skill")
This works fine, but now when I change one of the skills:
programmer = Programmer(python_skill=1.0, stackoverflow_skill=9.0)
print(programmer.total_score) # return 10.0
programmer.python_skill=2.0
print(programmer.total_score) # still return 10.0
I would like the total_score to automatically update.
Any solutions?
TNX!!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…