简体   繁体   中英

How to access all repeated structured properties in GAE ndb Python

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

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

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.

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