[英]How do I populate a StructuredProperty from a dict?
在StructuredProperty
上没有populate
方法(如ndb.Model
上的populate
方法),那么如何从dict中填充这些字段?
You can still populate
the StructuredProperty
. 您仍然可以
populate
StructuredProperty
。
If you have models like this: 如果您有这样的模型:
class A(ndb.Model):
value = ndb.IntegerProperty()
class B(ndb.Model):
name = ndb.StringProperty()
a = ndb.StructuredProperty(A)
The following will populate the properties of both: 以下将填充这两个属性:
my_dict = {"name":"my name", "a":{"value":1}}
b = B()
b.populate(**my_dict)
You can also call populate
on the property: 您还可以在属性上调用
populate
:
my_dict = {"value":1}
b = B()
b.a = A()
b.a.populate(**my_dict)
Note that what is returned by the getter is not the StructuredProperty
instance. 请注意,getter返回的不是
StructuredProperty
实例。 It is an instance of A
. 它是
A
一个实例。 So calling populate
works. 因此,调用
populate
工程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.