I’m thoroughly confused by the way namespaces work in App Engine (python). I’ve read https://developers.google.com/appengine/docs/python/multitenancy/multitenancy#Using_Namespaces_with_the_Datastore and followed its suggestions, but why does this code work? Shouldn’t it use the “current” namespace in the “get”?
namespace_manager.set_namespace("foo")
t = model.Track(description="in foo namespace")
t.put()
namespace_manager.set_namespace("bar")
# Why doesn't this line fail?
x = model.Track.get(t.key())
x.delete()
The namespace is part of the key.
The namespace you define with the namespace manager is only taken into account when you don’t specify another one explicitly, e.g. when creating a new entity. In other words, when a new key is being generated, the namespace you’ve set with namespace_manager.set_namespace is injected into the key. From then on, the key contains the complete information as to the “location” of the entity (even the app id) and it’s sufficient to retrieve it.
Read at https://developers.google.com/appengine/docs/python/datastore/keyclass#Key_namespace