Parse_obj在pydantic中带有异质元组的田地?
在pydantic中,
class Foo(BaseModel):
bar : str
baz : int
可以从元组中导入[“ AAA”,3]
通过做类似的操作:
[**{key: tr[i] for i, key in enumerate(fields.__TraceItem__.keys())})]
也可以从{bar:“ AAA”,BAZ:3} <
{bar:bar:3} < /代码>使用
parse_obj
。但是,如何导入将两者结合在一起的东西呢?换句话说,给定类别
class Bar(BaseModel):
f1: str
f2: float
f3: Boolean
class Qux(BaseModel):
field1: str
field2: float
field3: list[Bar]
如何将以下JSON转换为上面的qux
对象?
{
field1: "bar",
field2: 3.14,
field3: [
["aaa", 2.71, True],
["bbb", -1, False]
]
}
In Pydantic the class
class Foo(BaseModel):
bar : str
baz : int
Can be imported from a tuple ["aaa", 3]
by doing something like:
[**{key: tr[i] for i, key in enumerate(fields.__TraceItem__.keys())})]
It can also be converted from {bar: "aaa", baz: 3}
using parse_obj
. But how do you import something that combines the two? In other words, given the classes
class Bar(BaseModel):
f1: str
f2: float
f3: Boolean
class Qux(BaseModel):
field1: str
field2: float
field3: list[Bar]
How do I convert the following JSON into the Qux
object above?
{
field1: "bar",
field2: 3.14,
field3: [
["aaa", 2.71, True],
["bbb", -1, False]
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
验证器 。
,“ 总会在Pydantic接受数据之前可能需要转换,这对于与模型不完全匹配的JSON数据尤其如此。
这
是原始值的列表,它绝对与
与实际
bar
对象列表相似的类型绝对不一样。对于这样的情况,您可能需要自己实施解析。
选项1
令
bar
具有验证器
,将列表列表解析为bar
的列表。如果传递的
value
是错误的数据格式:您在标题中提到了
parse_obj
,我假设您的意思是选项2
让
bar
具有验证器
与 option 1 相同,但使用enter_item = eyter_item = eyter_item = true
,以便您可以一次解析一个列表项目(一个三重态
一次)bar
对象:如果传递
value
是错误的数据格式:您在标题中提到
parse_obj
,我认为您的意思是parse_obj
助手功能。它也与此相同:
作为附带说明,我已经在评论中提到了它,您提到“ tuple ”
但这不是 ,我也不会在您的任何示例代码和输入/输出中看到任何元组数据。我不确定您是否知道,但是在python中,元组是指特定数据类型: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences 。
您所拥有的基本上是列表的列表,每个子列表只是其他数据类型的列表。 (这就是为什么在我的回答中,我将其称为
triplet
)。You can use a
validator
.As pointed out in a comment, "there will be always cases where a transformation might be necessary before pydantic can accept the data", which is especially true for your JSON data that does not exactly match the model.
This
is a list of primitive values and it is definitely not the same type as
which more closely resembles a list of actual
Bar
objects.For cases like that, you might need to implement the parsing yourself.
Option 1
Let
Bar
have avalidator
that parses a list of lists into a list ofBar
's.If the passed
value
is of the wrong data format:You mentioned
parse_obj
in your title, and I assume you mean theparse_obj
helper function.It works just as well with that:
Option 2
Let
Bar
have avalidator
same as Option 1, but witheach_item=True
so that you can parse one list item at a time (onetriplet
at a time) into aBar
object:If the passed
value
is of the wrong data format:You mentioned
parse_obj
in your title, and I assume you mean theparse_obj
helper function.It works just as well with that:
As a side note, I mentioned it already in a comment, you mention "tuple" in your question
But that is not a tuple, nor do I see any tuples in any of your example code and input/output data. I'm not sure if you are aware, but in Python, a Tuple refers to a specific data type: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences.
What you have is basically a list of lists, and each sub-list is just list of other data types. (Which is why in my answer I refer to it as a
triplet
).我会采用一个更简单的方法,基于 gino的答案 -
bar bar
添加类方法要允许从数组实例化,然后从qux
>从验证器调用该方法:这是在行动中看起来:
I would take a simpler approach, based off of Gino's answer – add a class method on
Bar
to allow instantiating from an array, then call that method from a validator fromQux
:Here's what it looks in action: