如何在Python GAE中设置嵌套的StructuredProperty对象的值

时间:2013-10-19 07:08:42

标签: python google-app-engine google-cloud-datastore

在Python(GAE)中,如何使用追加在以下数据结构中设置“a”的值:

class Y(ndb.Model): 
    name = ndb.StringProperty() 
    hobby = ndb.StringProperty() 

class X(ndb.Model): 
    a = ndb.StructuredProperty(Y, repeated=True) 

使用下面的表单不起作用(在“数据存储查看器”中没有任何内容存储为已验证):

new_user_record = X() 
new_user_record.a.append({'name':"john", 'hobby':"painting"}) 
new_user_record.put() 

我做错了吗?什么是正确的方法?

1 个答案:

答案 0 :(得分:2)

class X(ndb.Model):
    a = ndb.StringProperty()

class Y(ndb.Model):
    b = ndd.StructuredProperty(X,repeated=True)



x = Y()
x.b = [X(a="123"),X(a="abc")]
test> x
Y(b=[X(a='123'), X(a='abc')])

并附加

test> x.b.append(X(a='xxx'))
test> x.b
[X(a='123'), X(a='abc'), X(a='xxx')]

查看文档,它确实显示了您 - https://developers.google.com/appengine/docs/python/ndb/properties#structured