在谷歌的雲數據存儲我的代碼等價於:App Engine的嵌套實體列表
req = datastore.CommitRequest()
req.mode = datastore.CommitRequest.NON_TRANSACTIONAL
foo = req.mutation.insert_auto_id.add()
barListProperty = foo.property.add()
barListValue = []
for i in range(5):
ent = datastore.Entity()
a = ent.property.add()
set_property(a, 'a', 1)
b = ent.property.add()
set_property(b, 'b', i)
set_property(barListProperty, 'barlist', barListValue)
key = datastore.Key()
path_element = key.path_element.add()
path_element.kind = 'Foo'
foo.key.CopyFrom(key)
print datastore.commit(req)
現在我想建立在NDB同樣的事情,所以我寫了這一點:
class Foo(Expando):
pass
foo = Foo()
foo.barlist = [Expando(a=1, b=i) for i in range(5)]
foo.put()
但我得到以下錯誤:
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~detect-analyze-notify-01a/sjuul.373145649709860280/main.py", line 317, in get
foo.put()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3339, in _put
return self._put_async(**ctx_options).get_result()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 325, in get_result
self.check_success()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 368, in _help_tasklet_along
value = gen.throw(exc.__class__, exc, tb)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/context.py", line 748, in put
key = yield self._put_batcher.add(entity, options)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 368, in _help_tasklet_along
value = gen.throw(exc.__class__, exc, tb)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/context.py", line 280, in _put_tasklet
keys = yield self._conn.async_put(options, datastore_entities)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 454, in _on_rpc_completion
result = rpc.get_result()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 612, in get_result
return self.__get_result_hook(self)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 1818, in __put_hook
self.check_rpc_success(rpc)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 1333, in check_rpc_success
raise _ToDatastoreError(err)
BadRequestError: BLOB, ENITY_PROTO or TEXT properties must be in a raw_property field.
我該怎麼辦?
編輯:這沒工作,要麼
class Foo(Expando):
pass
class Bar(Expando):
pass
foo = Foo()
foo.barlist=[Bar(a=1, b=i) for i in range(5)]
foo.put()
你嘗試保存到數據存儲?我得到同樣的例外。請參閱我的帖子的編輯以瞭解我使用過的代碼。注意順便說一句,在'foo.put()' –
引發異常是的,你是正確的。我剛剛閱讀了Expando課程的來源,我不相信你現在可以做到你想做的。目前,如果屬性是Model(即結構化)屬性,它將創建一個StructuredProperty,如果它是一個列表,它將它包裝在GenericProperty中,但嵌套Expando似乎沒有處理。儘管你可以在StructuredProperty中重複一個Expando。 –
在這個問題上看到它的一個例子http://stackoverflow.com/questions/13631884/ndb-querying-a-genericproperty-in-repeated-expando-structuredproperty –