pydantic basemodel“ field”仅出于验证目的
考虑说明使用pydantic
basemodel
的follwoing代码:
from pydantic import BaseModel, validator
class User(BaseModel, frozen=True):
id_key: int
user_id: int
@validator('user_id')
def id_check(cls, v, values):
if v > 2 * values['id_key'] + 1:
raise ValueError('id check failed.')
return v
user_dict = {'user_id': 10, 'id_key': 60}
u = User(**user_dict)
现在,在我的应用程序中,我真的不希望id_key
是一个u
之类的模型实例中的常规,可访问的字段 - 其唯一目的是验证user_id
。就我的示例而言,是否有一种方法可以访问id_key
用于验证目的,但没有标准字段?
Consider the follwoing code illustrating use of the pydantic
BaseModel
with validation:
from pydantic import BaseModel, validator
class User(BaseModel, frozen=True):
id_key: int
user_id: int
@validator('user_id')
def id_check(cls, v, values):
if v > 2 * values['id_key'] + 1:
raise ValueError('id check failed.')
return v
user_dict = {'user_id': 10, 'id_key': 60}
u = User(**user_dict)
Now, in my application, I don't really want id_key
to be a regular, accessible field in model instances like u
--its sole purpose is for validating user_id
. For my example, is there a way to have access to id_key
for validation purposes but not have it be a standard field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
value
您在id_check
函数中具有的参数是属性的内部dict 已经验证的实例,如果您需要具有id_key
仅在实例化时间进行检查,而不是在此之后,您可以简单地从value
中删除它。我想为您的代码提出一个其他改进:在当前状态下,pydantic在返回验证错误之前运行所有字段的验证,如果您将某些内容完全无效,则
> id_key
>例如,例如“ ABC”,或省略它,它不会添加到value
,user_id
的验证将使用keyError崩溃:'id_key'
,吞咽所有验证过程的其余部分,并没有返回明智的消息。这不是很明确,如果您期望pydantic
vilenationError
,可能会导致应用程序问题。您可能需要检查id_key
确实存在于value
中,并清洁错误(如果不是)。The
values
argument you have in yourid_check
function being the internal dict of attributes already validated for your instance, if you need to haveid_key
only at instantiation time for checking and not after that, you could simply remove it fromvalues
.There is one additional improvement I'd like to suggest for your code: in its present state, as pydantic runs the validations of all the fields before returning the validation errors, if you pass something completely invalid for
id_key
like "abc" for example, or omit it, it won't be added tovalues
, and the validation ofuser_id
will crash withKeyError: 'id_key'
, swallowing all the rest of the validation process and returning no sensible message.This is not very explicit, and might cause issues with your application if you expect a pydantic
ValidationError
. You might want to check thatid_key
is indeed present invalues
and raise the error cleanly if not.