此功能在交互式控制台上运行良好:
from google.appengine.api import namespace_manager
from google.appengine.ext import db
namespace_manager.set_namespace("some_namespace")
class Class(db.Model):
c = db.StringProperty()
x = Class(c="text")
x.put()
但是当登录执行时
namespace_manager.set_namespace(user.namespace)
检索和存储在数据存储中的所有数据都属于根(空)命名空间。提出问题
请不要将我指向此链接:https://developers.google.com/appengine/docs/python/multitenancy/multitenancy文档非常...
编辑
这回答了这个问题
"set_namespace(namespace) Sets the namespace for the current HTTP request."
我想“为什么留言簿示例不同”的答案在
appengine_config.py
中.现在唯一的问题是 - 当登录用户时,他必须能够读取根命名空间,所以表面上我必须将用户数据存储在根命名空间中,但是一旦他登录并且他的命名空间设置为特定的东西,我的 cookie 检查功能无法访问根命名空间并导致错误。
我该如何解决? (感觉像是在自言自语)
最佳答案
您将需要在处理程序函数中设置命名空间,因为如果您在导入的正下方设置命名空间,则该部分代码将被缓存,并且不会为每个请求重新执行。如果您将其设置在代码的非动态部分,则相同。
所以我认为发生的是第一次加载代码时没有用户并且命名空间不会改变。当然它在交互式控制台中工作,因为代码的整个部分都被执行了。
# this will be the namespace of the user when the code loads or nothing
# and it will never change as long as the instance is up
namespace_manager.set_namespace(user.namespace)
class YourHandler(webapp2.RequestHandler):
def get(self):
# get the user....
namespace_manager.set_namespace(user.namespace)
# setting the namespace here will change it for each request.
关于python - 设置 GAE 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15888643/