当我唯一的输入是 ndb.Query 时,如何获得 ndb.Model? [英] How can I get the ndb.Model when my only input is an ndb.Query?
本文介绍了当我唯一的输入是 ndb.Query 时,如何获得 ndb.Model?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设有如下所示的 ndb.Model
:
class Foo(ndb.Model):
bar = ndb.StringProperty()
我的问题是,如果我唯一的输入是 Foo.query()
我怎样才能将模型作为该查询所属的对象?
My question is, if my only input is the Foo.query()
how can I get the model as an object that this query belongs to?
def query_to_model(query):
# some magic
return model
<小时>
Foo.query().kind
将模型的名称作为字符串返回,但我没有设法找到将其作为对象获取的方法.
The Foo.query().kind
return the model's name as a string, but I didn't manage to find a way to get it as an object.
以下使用 eval
工作,但仅当模型在同一文件中定义时:
The following works using eval
, but only when the model is defined in the same file:
def query_to_model(query):
return eval(query.kind)
我想要更一般的东西.
推荐答案
导入具有此模型定义的代码后,列表 ndb.Model._kind_map
应包含它.这就是魔法:
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]
这篇关于当我唯一的输入是 ndb.Query 时,如何获得 ndb.Model?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文