python - 设置 GAE 命名空间

标签 python google-app-engine namespaces

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

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

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

  • 请不要将我指向此链接: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/

    相关文章:

    python - Matplotlib:让 contourf 颜色标签以对数刻度和多个级别显示

    python - 在numpy中将一维数组膨胀为二维数组

    java - App Engine - 后台线程不工作

    scala案例对象污染

    python - 在 sudo 下运行 python 脚本时,如何避免以 root 身份创建文件夹?

    python - 从行向量创建 block 矩阵的最佳方法是什么?

    google-app-engine - Google App Engine HRD 迁移需要多长时间?

    android - 使用 Google Cloud/Android Mobile Backend Starter 上的 Kind Name 访问范围

    ruby-on-rails - Rails 命名空间路由和资源上的自定义操作

    python - 继承中命名空间的顺序是什么?