Edmund Sulzanok
Edmund Sulzanok

Reputation: 1973

setting GAE namespace

This function works fine on Interactive console:

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()

but when login executes namespace_manager.set_namespace(user.namespace) all data retrieved and stored in datastore belongs to root (empty) namespace.

that raises questions

  1. am i setting a namespace wrong?
  2. do i have to set it each time just before i retrieve and store data (that wan't the case in guestbook example)
  3. if namespece is set on server side how does it know which post/get() belongs to which namespace?

Please dont point me to this link: https://developers.google.com/appengine/docs/python/multitenancy/multitenancy the documentation is very...

EDIT this answers the question

"set_namespace(namespace) Sets the namespace for the current HTTP request."

And i guess the answer to "why was guestbook example different" is in appengine_config.py.

Now the only question is - when logging in a user he must be able to read root namespace, so appearantly i must store user data in root namespace, but once he is logged in and his namespace is set to something specific, my cookie check function can't access root namespace and causes error.

How do I get around that? (feel like talking to myself)

Upvotes: 0

Views: 217

Answers (1)

aschmid00
aschmid00

Reputation: 7158

you will need to set the namespace within a handler function because if you set a namespace for example right under your imports that part of code will be cached and wont be re-executed for every request. the same if you set it in a non dynamic portion of your code.

so what i think happens is that the first time the code loads there is no user and the namespace wont change. of course it works in the interactive console because the whole part of code gets exectuted.

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

Upvotes: 1

Related Questions