How can I get the ndb.Model when my only input is an ndb.Query?

主宰稳场 提交于 2019-11-29 16:46:32
Greg

After you have imported code with this model definition, the list ndb.Model._kind_map should contain it. Here is the magic:

def query_to_model(query):
  return ndb.Model._kind_map[query.name]

I use this code to find the model class if you have the kind name:

model_module = KIND_MODULES(kind_name)
mod = __import__(model_module, globals(), locals(), [kind_name], -1)
model_class = getattr(mod, kind_name)

The KIND Modules dict holds the modules to import the models from:

KIND_MODULES = { 'Users' : 'models', 'Comments' : 'models', 'Cities' : 'examples.models' }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!