Drathier
Drathier

Reputation: 14519

How do I populate a StructuredProperty from a dict?

There is no populate method on StructuredProperty (like the one on ndb.Model) so how do I populate those fields from a dict?

Upvotes: 1

Views: 176

Answers (1)

murat
murat

Reputation: 4963

You can still populate the 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:

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. It is an instance of A. So calling populate works.

Upvotes: 1

Related Questions