【问题标题】:AppEngine: Model is not immutableAppEngine:模型不是不可变的
【发布时间】:2014-07-05 15:29:42
【问题描述】:

我正在尝试将模型实例插入 ndb 数据库。

它不断给我“模型不是不可变的”错误。

我尝试了不同的型号名称,但仍然是同样的错误。

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

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    您需要使用futures 列表,不是 entities 等待异步过程完成:

    ndb.Future.wait_all(futures)
    

    wait_all() 函数将这些存储在set() 对象中,并且集合要求内容是可散列的。由于可变对象不能存储在集合或字典中,Google 工程师在 Model.__hash__ 方法中添加了显式 TypeError,这就是您在此处看到的。

    【讨论】:

    • 太棒了!我的错!
    猜你喜欢
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    相关资源
    最近更新 更多