如何建模Pydantic模型以接受IP作为DICE或CIDR字符串
在Pydantic中,是否可以通过而不是dict 的值,仍然可以通过基本模型?
我有一种情况,我希望能够处理CIDR格式化的IP(例如1.2.3.4/32)并仍然返回有效的模型IPv4。
在下面的示例中,我初始化了3个IP。 对于第三IP,我通过了CIDR格式的STR,并希望能够返回有效的IPv4模型。
@Root_Validator仅用于打印传递的值。
您可以看到,键“ IP3”的第三值未由类处理。错误是
pydantic.Error_wrappers.ValidationError:IPS的1个验证错误 ip3
值不是有效的dict(type = type_error.dict)
代码
from pydantic import BaseModel, root_validator
class Ipv4(BaseModel):
"""
Validate structure of IPv4
"""
address: str
subnet_mask: int = 22
@root_validator(pre=True)
def handle_address_from_cidr_notation(cls, values):
print(f'These are the values passed into the model: {values}')
return values
class Ips(BaseModel):
ip1: Ipv4
ip2: Ipv4
ip3: Ipv4
ips_dict = {
'ip1': {'address': '1.1.1.1', 'subnet_mask': 24},
'ip2': {'address': '1.1.1.1'},
'ip3': '1.1.1.1',
}
ips: Ips = Ips(**ips_dict)
print(ips.ip1)
print(ips.ip2)
print(ips.ip3)
输出
These are the values passed into the model: {'address': '1.1.1.1', 'subnet_mask': 24}
These are the values passed into the model: {'address': '1.1.1.1'}
Traceback (most recent call last):
File "playground/test_pydantic_13.py", line 30, in <module>
ips: Ips = Ips(**ips_dict)
File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Ips
ip3
value is not a valid dict (type=type_error.dict)
In Pydantic, is it possible to pass a value that is not a dict and still make it go through a BaseModel?
I have a case where I want to be able to process a CIDR formatted IP (e.g. 1.2.3.4/32) and still return a valid model Ipv4.
In the example below I initialize 3 IPs.
For the 3rd IP I pass a CIDR formatted str and want to be able to return a valid Ipv4 model.
the @root_validator is only used to print the passed values.
You can see that the 3rd value for key 'ip3' is not processed by the class. The error is
pydantic.error_wrappers.ValidationError: 1 validation error for Ips
ip3
value is not a valid dict (type=type_error.dict)
Code
from pydantic import BaseModel, root_validator
class Ipv4(BaseModel):
"""
Validate structure of IPv4
"""
address: str
subnet_mask: int = 22
@root_validator(pre=True)
def handle_address_from_cidr_notation(cls, values):
print(f'These are the values passed into the model: {values}')
return values
class Ips(BaseModel):
ip1: Ipv4
ip2: Ipv4
ip3: Ipv4
ips_dict = {
'ip1': {'address': '1.1.1.1', 'subnet_mask': 24},
'ip2': {'address': '1.1.1.1'},
'ip3': '1.1.1.1',
}
ips: Ips = Ips(**ips_dict)
print(ips.ip1)
print(ips.ip2)
print(ips.ip3)
Output
These are the values passed into the model: {'address': '1.1.1.1', 'subnet_mask': 24}
These are the values passed into the model: {'address': '1.1.1.1'}
Traceback (most recent call last):
File "playground/test_pydantic_13.py", line 30, in <module>
ips: Ips = Ips(**ips_dict)
File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for Ips
ip3
value is not a valid dict (type=type_error.dict)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在Python 3.10上使用Pydantic 1.9
对于使用Pydantic 1.9的Python 3.6,使用
模型IPv4CIDR提供了一种验证方法,该方法将CIDR格式化的str分配以解决和subnet_mask并将新值传递给其继承的模型IPv4。
这允许以3种不同的方式传递IP:
的CIDR符号代码输出。
Python 3.10 + Pydantic 1.9
Python 3.6 + Pydantic 1.9
This works on Python 3.10 with pydantic 1.9
For Python 3.6 with pydantic 1.9, There is an error when using
Model Ipv4Cidr provides a validate method that splits the cidr formatted str to address and subnet_mask and passes the new values to Model Ipv4 which it inherits.
This allows for passing the IP in 3 different ways:
Code
Output for Python 3.10 + Pydantic 1.9
Output for Python 3.6 + Pydantic 1.9