使用条件创建Python数据模型
如何使用条件创建 pydantic模型?
我有一个pydantic模型。当以此模型为基础新课程时,它运行良好。我遇到的问题是,
baseuser.username
应该有- 32个字符的最大
- 最低长度 6个字符。
baseuser.password
要固定为64个字符。不少,不多。
这是我可以通过数据型实现的吗?
>我知道我可以用简单的“如果”语句做到这一点,但是我希望它是pydantic模型中的数据类型。
from pydantic import BaseModel
class BaseUser(BaseModel):
id: int
username: str
password: str
谢谢。。
How do I create pydantic model with conditions?
I have a pydantic model. It works well when declaring new class with this model as base. The problem I have is, the
BaseUser.username
should have- maxlength of 32 chars
- minlength of 6 chars.
BaseUser.password
to be fixed to 64 chars. Not less, not more.
Is that any way that I can achieve it through datatypes?
> I know that I can do it with simple "if" statements, but I want it to be a datatype in pydantic model.
from pydantic import BaseModel
class BaseUser(BaseModel):
id: int
username: str
password: str
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过使用“ nofollow noreferrer”> contrast类型?
样本输出
只是一个抬头:我以前从未使用过Pydantic。因此,可以有更好的方法来解决这个问题。
Have you considered using Contraint types?
Sample output
Just a heads-up: I have never used Pydantic before. So, there could be a better way to solve this.