How to remove built-in kinds' names in google

2019-09-14 14:24发布

I am trying to use google cloud datastore kind query to get a list of kind names as demoed in the Kind queries,

query = client.query(kind='__kind__')
query.keys_only()

kinds = [entity.key.id_or_name for entity in query.fetch()]

but the code generates some built-in kind names, e.g.

['_AE_DatastoreAdmin_Operation', '_GAE_MR_TaskPayload',
 '__Stat_Kind_IsRootEntity__', '__Stat_Kind_NotRootEntity__',
 '__Stat_Kind__', '__Stat_PropertyName_Kind__', 
 '__Stat_PropertyType_Kind__', '__Stat_PropertyType_PropertyName_Kind__',
 '__Stat_PropertyType__', '__Stat_Total__'] 

I am wondering how to remove these built-in kind names and only retain user created kind names.

1条回答
可以哭但决不认输i
2楼-- · 2019-09-14 14:53

Those appear to be kinds of real entities created on the local development server/emulator - they can actually be seen in the Datastore Viewer. For example the __Stat_* ones are created when the datastore Generate Stats action is performed on the local development server.

These entities do not exist in the project's live cloud datastore (or they are stored elsewhere).

With a simple naming rule for the application's entity kinds - to not start with the _ character - you could obtain the kinds list like this:

kinds = [entity.key.id_or_name for entity in query.fetch()
             if not entity.key.id_or_name.startswith('_')]

Depending on the kinds use, another option - safer IMHO from coding perspective - might be to always check the kind names against an explicit expected list (for example when wiping out all kinds entities):

kinds = [entity.key.id_or_name for entity in query.fetch()
             if entity.key.id_or_name in known_kinds_list]
查看更多
登录 后发表回答