问题
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:
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]
来源:https://stackoverflow.com/questions/42379567/how-to-remove-built-in-kinds-names-in-google-datastore-using-kind-queries