2012-09-16 28 views
1

這裏是StructuredProperty from the docs一個例子:如何檢索和操作ndb StructuredProperty對象?

class Address(ndb.Model): 
    type = ndb.StringProperty() # E.g., 'home', 'work' 
    street = ndb.StringProperty() 
    city = ndb.StringProperty() 

class Contact(ndb.Model): 
    name = ndb.StringProperty() 
    addresses = ndb.StructuredProperty(Address, repeated=True) 

guido = Contact(name='Guido', 
       addresses=[Address(type='home', 
            city='Amsterdam'), 
          Address(type='work', 
            street='Spear St', 
            city='SF')]) 

guido.put() 

試想,圭多暫時在城市廷巴克圖,馬裏的工作。我將如何去檢索和更新他的工作地址?

謝謝。

回答

3

我會嘗試這樣的事情。

for address in guido.addresses: 
    if address.type == 'work': 
     address.street = "Main Street" 
     address.city = "Timbuktu" 

guido.put() 

EDIT 加入冒號

+0

謝謝,@Hans然後。你錯過了一個冒號,但是否則代碼工作:) –

+0

修正了冒號:-) –