How do I get a list of namespaces on google app en

2019-03-15 09:05发布

I would like to make a backup of all user data in the datastore. My application is using the new namespace feature to provide multi tenanting on a per user basis (as per the example in the docs).

The bulk loader needs the namespace for each customer to download the data. I don't keep a list of users, so I can't generate the namespaces. Is there a method of detecting all the currently used namespaces?

4条回答
聊天终结者
2楼-- · 2019-03-15 09:35

There is also now a get_namespaces() function:

from google.appengine.ext.db import metadata

namespaces = metadata.get_namespaces() 

get_namespaces() returns a list of Namespace objects. The docs also note that "metadata queries that fetch information on namespaces, kinds, and properties are generally slow to execute."

查看更多
相关推荐>>
3楼-- · 2019-03-15 09:42

Using ndb

from google.appengine.ext.ndb import metadata
all_namespaces = [ns for ns in metadata.get_namespaces()]

Using datastore

Per Datastore Metadata:

query = client.query(kind='__namespace__')
query.keys_only()   
all_namespaces = [entity.key.id_or_name for entity in query.fetch()]
查看更多
Root(大扎)
4楼-- · 2019-03-15 09:51

There's no API to get a list of namespaces. You must keep a record of the ones you use. I use a model specifically for this.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-03-15 09:57

Since SDK 1.4.0 you can use Metadata Queries:

from google.appengine.ext.db import metadata

for ns in metadata.get_namespaces():
    print "namespace: '%s'" % ns.namespace_name

For NDB the import is slightly different:

from google.appengine.ext.ndb import metadata
查看更多
登录 后发表回答