如何在NDB数据存储中获取模型列表

Eya*_*vin 1 app-engine-ndb google-cloud-datastore

我想以编程方式删除数据存储区中的所有数据。

为此,我需要遍历所有模型,并为每个模型删除所有模型的实体。

那么,如何以编程方式获取NDB数据存储区中所有模型的列表?

dle*_*ech 5

使用元数据。我自己也需要同样的功能,因此这里是所有型号的完整删除功能。每个模型最多可以处理数千个实体:

from google.appengine.ext import ndb
from google.appengine.ext.ndb import metadata

kinds = metadata.get_kinds()
for kind in kinds:
    if kind.startswith('_'):
        pass  # Ignore kinds that begin with _, they are internal to GAE
    else:
        q = ndb.Query(kind=kind)
        keys = q.fetch(keys_only=True)

        # Delete 1000 entities at a time.
        for i in range(len(keys) / 1000 + 1):
            portion = keys[i*1000: i*1000+1000]
            ndb.delete_multi(portion)
Run Code Online (Sandbox Code Playgroud)