Home > OS >  Get all entities for namespace -- KindError while trying to fetch all entities of a namespace from d
Get all entities for namespace -- KindError while trying to fetch all entities of a namespace from d

Time:11-03

I try to get all entities from a namespace in order to delete them in a later step. Im using appengine together with datastore and ndb lib using python2.7

I have a simple query to get all entities:

    def get_entities(namespace_id):

        return [entity for entity in ndb.Query(namespace=namespace_id).fetch()]

Also modified it to avoid the dunder kinds/entities Datastore Statistics in legacy bundled services:

    def get_entities(namespace_id):

        return [entity for entity in ndb.Query(namespace=namespace_id).fetch() if not entity.key.id_or_name.startswith('__')]

While running locally using datastore emulator works just fine. But I get this error when deployed in the cloud:

KindError: No model class found for kind '__Stat_Ns_Kind_IsRootEntity__'. Did you forget to import it?

I found this post Internal Kinds Returned When Retrieving All Entities Belonging to a Particular Namespace but not a clear answer.

If you have another way to get all the entities fro a specific namespace will be welcome!!

CodePudding user response:

Per the documentation you have referenced, it's the kind name that begins and ends with two underscores.

Each statistic is accessible as an entity whose kind name begins and ends with two underscores

However, your code is checking for entity keys that starts with an underscore. You should be checking the kinds instead

Modify your code to

return [entity for entity in ndb.Query(namespace=namespace_id).fetch(keys_only=True) if not entity.kind().startswith('__')]

Note: I switched your query to only fetch keys since all you want is to delete the records

  • Related