[英]How to get all class properties (in Python) of a GAE polymodel including those defined via @property
I am using the Google App Engine polymodel to model data that can have more than one instance of a property - eg a contact could have multiple phone numbers. 我正在使用Google App Engine多模型来建模可以拥有多个属性实例的数据 - 例如,联系人可能有多个电话号码。 Say this is my setup:
说这是我的设置:
class Foo(polymodel.PolyModel):
some_prop = ndb.StringProperty()
@property
def bar(self):
return Bar.query(Bar.foo == self.key)
class Bar(ndb.Model):
foo = ndb.KeyProperty(kind = Foo)
other_prop= ndb.StringProperty()
(I got this approach after reading this GAE article on data modeling: https://developers.google.com/appengine/articles/modeling ) (在阅读关于数据建模的GAE文章后,我得到了这种方法: https : //developers.google.com/appengine/articles/modeling )
Now when I do: 现在我做的时候:
Foo._properties
I only get access to the following: 我只能访问以下内容:
{'some_prop': StringProperty('some_prop'),
'class': _ClassKeyProperty('class', repeated=True)}
Is there any way to access to ALL properties, including those defined with "@property"? 有没有办法访问所有属性,包括那些用“@property”定义的属性?
Many thanks for any help or or insight on where I'm going wrong. 非常感谢您对我出错的地方有任何帮助或洞察力。 - Lee
- 李
UPDATE: Based on @FastTurle's great answer, I've now added a class method that returns both class properties as well as methods tagged as properties via @property: 更新:根据@ FastTurle的好答案,我现在添加了一个类方法,它返回类属性以及通过@property标记为属性的方法:
def props(self):
return dict(self._properties.items() + \
{attr_name:getattr(self,attr_name) for \
attr_name, attr_value in \
Foo.__dict__.iteritems() if \
isinstance(attr_value,property)}.items())
Performing Foo._properties
should get you access to any attributes you define on your polymodel.PolyModel
that inherit from google.appengine.ext.db.Property
. 执行
Foo._properties
应该让你获得你所定义你的任何属性polymodel.PolyModel
从继承google.appengine.ext.db.Property
。
This means that KeyProperty
, StringProperty
, etc... will show up in Foo._properties
, so I'm assuming you now need to find all the methods decorated by property
. 这意味着
KeyProperty
, StringProperty
等将出现在Foo._properties
,所以我假设你现在需要找到property
修饰的所有方法。
Fortunately, this isn't that hard. 幸运的是,这并不难。 First, a quick sidetrack into decorators (forgive me if you know this already).
首先,快速了解装饰者(如果你已经知道这一点,请原谅我)。
In python decorators are just syntactic sugar . 在python中, 装饰器只是语法糖 。 For example, these two methods result in the same thing:
例如,这两种方法导致相同的结果:
@property
def bar(self):
pass
def bar(self):
pass
bar = property(bar)
Fortunately for us, property(obj)
returns a new property
object. 对我们来说幸运的是,
property(obj)
返回一个新的property
对象。 This also means that @property
returns a property
object as well. 这也意味着
@property
也会返回一个property
对象。 You can kind of think of property
as a class! 您可以将
property
视为一个阶级! And finally can use isinstance(bar, property)
to see if bar
is a property. 最后可以使用
isinstance(bar, property)
来查看bar
是否属性。
Finally, we can put this into use by examining each of Foo
's attributes and selecting only those that are property
instances. 最后,我们可以通过检查
Foo
的每个属性并仅选择那些property
实例来使用它。
for attr_name, attr_value in Foo.__dict__.iteritems():
if isinstance(attr_value, property):
print attr_name, attr_value
# bar, <property object at ...>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.