Reputation: 1185
class X(ndb.Model):
content = ndb.StringProperty()
number = ndb.IntegerProperty()
class P(ndb.Model):
unit = ndb.StructuredProperty(X, repeated=True)
Using Datastore viewer, i could see the following stored data:
Entity Kind P
Entity Key ag1kZXZ-bGlmZXN3YWxschoLEg1BZHZlcnRpc2VtZW50IGCAgICA4NcKDA
ID 6015428115566296
unit.content (list) [u'Apple', u'Coca Cola', u'Orange', u'Audi']
unit.number (list) [10L, 5L, 10L, 10L]
Goal: To access the different unit.content values (like, 'Apple', etc)
[i am a novice in GAE Python; so please excuse if the question is too silly]
My (unsuccessful)attempt[i tried a few other ways but in vain]:
unitv_query = P.query()
mv = unitv_query.fetch()
for a in mv.unit:
logging.info ("content=[%s]", a.content)
I get the following error:
::
File "C:\learn\eg5.py", line 495, in render_unit
for a in mv.unit:
AttributeError: 'list' object has no attribute 'unit'
Any help is appreciated
Upvotes: 1
Views: 2041
Reputation: 12986
unit
property will be a list of X's
however you are looping over the wrong things
You could should be,
unitv_query = P.query()
mv = unitv_query.fetch()
# mv is a list of P
for a in mv:
# now loop over each entity in unit property.
for i in a.unit:
logging.info ("content=[%s]", i.content)
In your code you are trying to access unit from mv
however mv is a list of P
entities - the result of the query and therefore you can't access unit
Upvotes: 3