【问题标题】:setting GAE namespace设置 GAE 命名空间
【发布时间】:2013-04-08 20:44:51
【问题描述】:

此功能在交互式控制台上运行良好:

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)时,所有检索到并存储在数据存储区中的数据都属于根(空)命名空间。

提出问题

  1. 我是否设置了错误的命名空间?
  2. 我是否必须在每次检索和存储数据之前设置它(在留言簿示例中不是这种情况)
  3. 如果在服务器端设置了 namespece,它如何知道哪个 post/get() 属于哪个命名空间?

请不要将我指向此链接:https://developers.google.com/appengine/docs/python/multitenancy/multitenancy 文档非常...

编辑 这回答了问题

"set_namespace(namespace) 设置当前HTTP的命名空间 请求。”

我想“为什么留言簿示例不同”的答案在 appengine_config.py

现在唯一的问题是 - 当登录用户时,他必须能够读取根命名空间,所以显然我必须将用户数据存储在根命名空间中,但是一旦他登录并且他的命名空间设置为特定的东西,我的cookie 检查功能无法访问根命名空间并导致错误。

我该如何解决? (感觉像是在自言自语)

【问题讨论】:

    标签: python google-app-engine namespaces


    【解决方案1】:

    您需要在处理程序函数中设置命名空间,因为如果您在导入的正下方设置命名空间,则该部分代码将被缓存并且不会为每个请求重新执行。如果您将其设置在代码的非动态部分中,则相同。

    所以我认为第一次加载代码时没有用户并且命名空间不会改变。当然它可以在交互式控制台中工作,因为整个代码部分都会被执行。

    # 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.
    

    【讨论】:

    • 是的,这确实有道理。我还发现,如果您有 appengine_cinfig.py 模块,则每次发出请求时都会调用 namespace_manager_default_namespace_for_request()(甚至是数据存储查询,因为我有一个请求,但该函数被调用了 4 次)并且它返回的任何字符串都已设置作为命名空间。这似乎是更优雅的解决方案。我用 namespace_manager.set_namespace("") user = Users.get_by_id(user_id) namespace_manager.set_namespace(user.namespace) 克服了 cookie 检查问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 2014-12-31
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    相关资源
    最近更新 更多