2014-07-05 26 views
0

我試圖將一個模型的實例插入ndb數據庫。AppEngine:模型不是不可變的

它一直給我「模型不是不可變的」錯誤。

我嘗試過不同的模型名稱,但仍然是相同的錯誤。

class User(ndb.Model): 
    username = ndb.StringProperty() 
    email = ndb.StringProperty() 
    lwr_username = ndb.ComputedProperty(lambda self: self.username.lower()) 
    lwr_email = ndb.ComputedProperty(lambda self: self.email.lower()) 

這裏是我的插入代碼:

entity = User() 
entity.email = "" 
entity.username = "bob" 

logging.info(entity) 

#Commit data asynchronously 
entities = [entity] 
futures = ndb.put_multi_async(entities) 

#Build Response whilst database is committing 
response = { 

} 

#Wait for commits to finish 
ndb.Future.wait_all(entities) 

這是一個完整的堆棧跟蹤

Model is not immutable 
Traceback (most recent call last): 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__ 
    rv = self.handle_exception(request, response, e) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__ 
    rv = self.router.dispatch(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher 
    return route.handler_adapter(request, response) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__ 
    return handler.dispatch() 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch 
    return self.handle_exception(e, self.app.debug) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch 
    return method(*args, **kwargs) 
    File "/base/data/home/apps/s~myapp-api/1.377037445874907069/v1/handler/userHandler.py", line 11, in post 
    responseCode, response = UserService.create(locale, json.loads(self.request.body), **kwargs) 
    File "/base/data/home/apps/s~myapp-api/1.377037445874907069/v1/service/userService.py", line 37, in create 
    ndb.Future.wait_all(entities) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/tasklets.py", line 345, in wait_all 
    waiting_on = set(futures) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 3017, in __hash__ 
    raise TypeError('Model is not immutable') 
TypeError: Model is not immutable 

回答

2

您需要使用futures列表,entities等待異步過程完成:

ndb.Future.wait_all(futures) 

wait_all()函數將這些函數存儲在一個set()對象中,並且設置要求內容可哈希。由於可變對象不能存儲在集合或字典中,因此Google工程師在Model.__hash__方法中添加了明確的TypeError,這是您在此處看到的內容。

+0

非常好!我的錯! – Chris