2

How to get the list of all NDB model names in GAE Python ?

All NDB models are Python classes which inherit from ndb.Model. I thought we could use this info to fetch the names of all models.

class BK (ndb.Model): 
    property_1 = ..

I tried below ( borrowed ) code but in vain :

ATTEMPT 1

logging.info ( [ cls.__name__ for cls in globals()['ndb.Model'].__subclasses__() ] )

It results in error :

KeyError: 'ndb.Model'

ATTEMPT 2

logging.info ( [ cls.__name__ for cls in globals()['Model'].__subclasses__() ] )

It results in error :

KeyError: 'Model'

Community
  • 1
  • 1
gsinha
  • 1,165
  • 2
  • 18
  • 43

2 Answers2

5

Fortunately for you, it is far easier in this case:

from google.appengine.ext import ndb


class Test(ndb.Model):
    pass

print ndb.Model._kind_map

Produces the following output:

{'Test': Test<>}
Jaime Gómez
  • 6,961
  • 3
  • 40
  • 41
3

In addition to Jaime's answer, there is also the metadata API, which can tell you the entity kinds that have been registered in the datastore.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Sorry but I marked Jaime's answer as accepted since I later figured out that the models for which entities have not yet been created do not get listed here. I overlooked `registered in the datastore`. My bad. – gsinha Mar 02 '15 at 19:28