Pydantic的V2 的 @field_validator

部署运行你感兴趣的模型镜像
@deprecated('Pydantic V1 style `@validator` validators are deprecated.'
    ' You should migrate to Pydantic V2 style `@field_validator` validators,'
    ' see the migration guide for more details', category=None)
def validator(__field: str,
              *fields: str,
              pre: bool = False,
              each_item: bool = False,
              always: bool = False,
              check_fields: bool | None = None,
              allow_reuse: bool = False) -> (_V1ValidatorType) -> _V1ValidatorType
 
Decorate methods on the class indicating that they should be used to validate fields.
形参:
__field – The first field the validator should be called on; this is separate from fields to ensure an error is raised if you don't pass at least one.
fields – Additional field(s) the validator should be called on.
pre – Whether this validator should be called before the standard validators (else after). Defaults to False.
each_item – For complex objects (sets, lists etc.) whether to validate individual elements rather than the whole object. Defaults to False.
always – Whether this method and other validators should be called even if the value is missing. Defaults to False.
check_fields – Whether to check that the fields actually exist on the model. Defaults to None.
allow_reuse – Whether to track and raise an error if another validator refers to the decorated function. Defaults to False.
返回值:
A decorator that can be used to decorate a function to be used as a validator.

  1. 警告的核心意思

在 Pydantic V1 中,我们习惯写验证器时用的是 @validator 装饰器,例如:

from pydantic import BaseModel, validator

class User(BaseModel):
    name: str

    @validator("name")
    def name_must_not_be_blank(cls, v):
        if not v.strip():
            raise ValueError("Name cannot be blank")
        return v

但是 在 Pydantic V2 中,这种写法已经被标记为 deprecated(弃用),会抛出你看到的这个警告。

在 V2 里,官方推荐用 @field_validator,写法变成:

from pydantic import BaseModel, field_validator

class User(BaseModel):
    name: str

    @field_validator("name")
    @classmethod
    def name_must_not_be_blank(cls, v):
        if not v.strip():
            raise ValueError("Name cannot be blank")
        return v

  1. 为什么要改?
    • V1 的 @validator:参数设计比较复杂(pre, each_item, always, check_fields 等),有时容易混淆。
    • V2 的 @field_validator:API 更清晰,和 @model_validator(整体验证器)区分开了。
    • 这样一来,维护和迁移时更直观,也符合 Pydantic V2 的整体设计。

  1. 警告的具体含义
    • @deprecated(…):告诉你 旧的 @validator 还能用,但不推荐继续使用。
    • 建议你按照 迁移指南 把代码里的 @validator 全部替换为 @field_validator。
    • 将来某个版本里,@validator 可能会被彻底移除。

✅ 总结:
这个警告就是在提醒你:你正在用 Pydantic V1 风格的 @validator,请改用 V2 的 @field_validator。

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonkeyKing.sun

对你有帮助的话,可以打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值