能,但必须用 model_validator(mode='after');它接收完整模型实例 self,可安全访问所有已解析字段,而 field_validator 默认仅限当前字段值,强行跨字段会报 AttributeError。
不能直接做——field_validator 默认只接收当前字段的值,不带 info.context 或完整模型实例,拿不到其他字段。硬写会报 AttributeError: 'ValidationInfo' object has no attribute 'data' 或类似错误。
真正该用的是 model_validator(mode='after'):它接收完整的模型实例(即 self),所有已解析

mode='after',否则默认是 'before',此时字段还没解析,self 里只有原始输入字典ValueError 或 AssertionError,Pydantic 会自动转成标准错误model_validator 不会执行——它只在所有字段通过基础校验后才运行from pydantic import BaseModel, model_validator
class Order(BaseModel):
paid: bool
amount: float | None = None
@model_validator(mode='after')
def check_amount_if_paid(self):
if self.paid and self.amount is None:
raise ValueError('amount is required when paid=True')
return self
旧版 Pydantic v1 有 values 参数,v2 中有人误以为 info.data 存在——其实没有。v2 的 ValidationInfo 对象只有 context、data 是被移除的属性,强行访问会报 AttributeError。
info.data['other_field'] —— 这段代码在 v2 中永远失败context 注入,但这是侵入式设计,不推荐用于字段间逻辑耦合model_validator,而不是拆到多个 field_validator 里互相猜状态比如“任意以 _url 结尾的字段,值必须以 https:// 开头”,这时不能硬编码字段名,得遍历 self.__pydantic_core_schema__?不用。更简单:直接读 self.__dict__ 或用 self.model_dump(),但注意前者含私有属性,后者触发序列化开销。
self.model_fields_set + self.__dict__ 筛出已设置的字段self.model_dump(exclude_unset=True) 获取干净字典model_validator 里调 self.model_dump_json() 这类重操作,容易引发循环或性能问题跨字段校验的本质不是“怎么取值”,而是“在哪取最安全”——答案始终是 model_validator(mode='after'),其他路子要么失效,要么埋雷。