How to list kinds in datastore?

2019-02-22 08:34发布

I just had to figure this out for my own application, so reposting the answer here.

3条回答
欢心
2楼-- · 2019-02-22 08:53
def GetSchemaKinds():
    """Returns the list of kinds for this app."""

    class KindStatError(Exception):
      """Unable to find kind stats."""

    from google.appengine.ext.db import stats
    global_stat = stats.GlobalStat.all().get()
    if not global_stat:
      raise KindStatError()
    timestamp = global_stat.timestamp
    kind_stat = stats.KindStat.all().filter(
        "timestamp =", timestamp).fetch(1000)
    kind_list = [stat.kind_name for stat in kind_stat
                 if stat.kind_name and not stat.kind_name.startswith('__')]
    kind_set = set(kind_list)
    return list(kind_set) 

Reference: http://groups.google.com/group/google-appengine/browse_thread/thread/f2e7568040c015ff

查看更多
小情绪 Triste *
3楼-- · 2019-02-22 08:56

It is worth noting that this answer is for the older db api. The new ndb api has another way to get all Kind listed here https://cloud.google.com/appengine/docs/python/ndb/metadata#get_kinds

查看更多
女痞
4楼-- · 2019-02-22 09:08

Time has passed since this was asked and answered. Now there's a simpler way.

See http://code.google.com/appengine/docs/python/datastore/metadataqueries.html

q = Kind.all()
for kind in q.fetch(100):
  print kind.kind_name
查看更多
登录 后发表回答