OK, I'm stuck. I have a simple StructuredProperty
(just one level deep) property on an entity. And at runtime, I want to get the value of one of the "sub-properties" on that StructuredProperty
by name. For example;
class Address(ndb.Model):
street = ndb.StringProperty()
# ...
class Person(ndb.Model):
# ...
address = ndb.StructuredProperty(Address)
and then at runtime I want to be able to do something like:
prop = entity._properties['address']
sub_prop = prop.__getattr__('street')
value = sub_prop.__get__(entity, Person)
or get at it using GenericProperty
or whatever works best. But I have tried a number of variations and I am clearly missing something. Of course the reason I want to do this is there are really a number of named Address
-like StructuredProperty
and I know which one I want based on runtime context.
Thanks for any help.
Bobby was right. I was getting at the property, not the property value the way I was thinking (long day).
The simple answer is
getattr(prop, 'some_name')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.