如何从dict填充StructuredProperty?

2024-04-28 08:45:28 发布

您现在位置:Python中文网/ 问答频道 /正文

StructuredProperty上没有populate方法(就像ndb.Model上的方法一样),那么如何从dict填充这些字段?在


Tags: 方法modeldictndbpopulatestructuredproperty
1条回答
网友
1楼 · 发布于 2024-04-28 08:45:28

你仍然可以populate这个StructuredProperty。在

如果你有这样的模型:

class A(ndb.Model):
  value = ndb.IntegerProperty()

class B(ndb.Model):
  name = ndb.StringProperty()
  a = ndb.StructuredProperty(A)

以下内容将填充这两个属性:

^{pr2}$

也可以对属性调用populate

my_dict = {"value":1}
b = B()
b.a = A()
b.a.populate(**my_dict)

注意,getter返回的不是StructuredProperty实例。它是A的一个实例。因此,调用populate起作用。在

相关问题 更多 >