简体   繁体   English

如何从字典填充StructuredProperty?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM