简体   繁体   English

通过getattr获取数据存储对象而不知道其类

[英]Get a datastore object by getattr without knowing its class

I want to get a entity through a ReferencePropery in this way. 我想以这种方式通过ReferencePropery获取实体。

getattr(model, refrence)

where model is a db.Model and reference is db.ReferenceProperty . 其中model是db.Model ,引用是db.ReferenceProperty But I get a KindError, which I can avoid by adding import of following sort. 但是我得到了KindError,可以通过添加以下种类的导入来避免。

from foomodule import ReferedKind

Where ReferedKind is the class of the entity I want to get by above getattr . 其中ReferedKind是我要在getattr之上获取的实体的类。

But my problem is in my case because of the nature of the function I want to call this in, the model can belong to any Model class of mine. 但是我的问题是由于要调用此函数的性质, model可以属于我的任何Model类。 So I don't know what the ReferedKind will be. 所以我不知道ReferedKind是什么。

How can I call getattr and get the referred entity without importing every possible model class? 如何调用getattr并获取引用的实体,而无需导入每个可能的模型类?

Is there a way I can know the class of a entity referred by a ReferanceProperty in advance so I can dynamically import it? 有没有一种方法可以提前知道ReferanceProperty引用的实体的类,以便可以动态导入它?

Aditional info 附加信息

I am writing a function that would return a json serializable python dict. 我正在编写一个将返回json可序列化的python字典的函数。 Because in some cases dictionary returned by db.to_dict() is not json serializable. 因为在某些情况下db.to_dict()返回的字典无法json序列化。 So the function doesn't know in advance what kind of a model it will have to put into a dict. 因此,该函数事先不知道将要放入字典的哪种模型。

Normally you need to import all potential models that you could retrieve before doing anything. 通常,您需要导入所有可以检索的潜在模型,然后再执行任何操作。

Your making things hard for yourself. 你让自己变得困难。

The only way you do it would be to work at a lower level than db.Model or ndb.Model to fetch the raw data then examine it then you will still need to import the model before dereferencing a referenceproperty or fetching a key. 唯一的方法是在比db.Model或ndb.Model更低的级别上工作以获取原始数据,然后对其进行检查,然后在取消引用属性或获取键之前,仍需要导入模型。

If you decide to do this ndm will be easier as it doesn't have a reference property, but a KeyProperty, you can use getattr, check if you have a key, then fetch the object referred to be the key. 如果决定执行此操作,则ndm会更容易,因为它没有引用属性,但是没有KeyProperty,则可以使用getattr,检查是否有键,然后获取称为键的对象。

If you want to stick with db. 如果您想坚持使用db。 it will be messy because you need to use get_value_for_datastore 这会很麻烦,因为您需要使用get_value_for_datastore

Which would involve, 其中涉及

getting the class of the entity, getting the property of the class and then calling get_value_for_datastore passing in the instance then assuming you have imported the you can get the key. 获取实体的类,获取类的属性,然后调用传入实例的get_value_for_datastore,然后假设已导入,则可以获取密钥。

so if you have an instance from the datastore called foo you would 因此,如果您有一个名为foo的数据存储区实例,

prop = getattr(foo.__class__,"the_property_name")
obj_key = prop.get_value_for_datastore(foo)

Then you will need to get the kind from the key, then work out how to import the model for that kind, before you get() the key, other wise you will still get the KindError 然后,您将需要从键中获取种类,然后弄清楚如何为该类型导入模型,然后再获取()键,否则,您仍将获得KindError

I am not sure all the effort is worth it, you will find it easier to just import the models. 我不确定所有的努力是否值得,您会发现导入模型更加容易。 You ultimately need to import any model before you use it. 您最终需要在使用任何模型之前将其导入。 The only thing you are might be gaining is a lazy import potentially saving some startup time. 您可能唯一获得的是惰性导入,可能节省一些启动时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不知道类名称的情况下获取类的属性 - How to get attributes of a class without knowing their names 在不知道其密钥的情况下从字典中获取价值的简单解决方案 - Simple solution to get value from dict without knowing its key 在不知道其父项的情况下获取dict项(Python) - Get dict item without knowing its parent (Python) Python:在没有类的情况下使用“ __getattr__”吗? - Python: using “__getattr__” without a class? 从python中的对象获取getattr属性 - get attribute with getattr from a object in python Python:如何用getattr得到一个object属性属性? - Python: How to get attribute of attribute of an object with getattr? 有没有办法在不知道参数的情况下从子 class 继承使用父 class 中的 function 参数的变量? - Is there a way to inherit a variable that uses a argument of its function in a parent class, from a child class without knowing the argument? 我如何在不获取AttributeError的情况下访问kivy中另一个类的对象属性:'super'对象没有属性'__getattr__' - How can i access object properties of another class in kivy without getting AttributeError: 'super' object has no attribute '__getattr__' 在不知道祖先的情况下从数据存储表中删除实体 - Delete entities from a datastore table without knowing ancestor 通过知道 class 名称,如何获取具有默认值的 class 密钥 | PYTHON - By Knowing class name, how to get class key with its default value | PYTHON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM