Google App Engine:如何以编程方式访问我的Model类的属性? [英] Google App Engine: how can I programmatically access the properties of my Model class?
本文介绍了Google App Engine:如何以编程方式访问我的Model类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class Person(db.Model):
first_name = db.StringProperty (required = True)
last_name = db.StringProperty(required = True)
这个类的实例在 p
中,而字符串 s
包含值'first_name'
。我想这样做:
print p [s]
和
p [s] = new_value
这两者都会导致 TypeError
。
有人知道我能达到我想要的吗? 如果 模型类是非常聪明的,它应该认识到Python的标准方法。
尝试:
getattr(p,s)
setattr(p,s,new_value)
还有hasattr可用。
I have a model class:
class Person(db.Model):
first_name = db.StringProperty(required=True)
last_name = db.StringProperty(required=True)
I have an instance of this class in p
, and string s
contains the value 'first_name'
. I would like to do something like:
print p[s]
and
p[s] = new_value
Both of which result in a TypeError
.
Does anybody know how I can achieve what I would like?
解决方案
If the model class is sufficiently intelligent, it should recognize the standard Python ways of doing this.
Try:
getattr(p, s)
setattr(p, s, new_value)
There is also hasattr available.
这篇关于Google App Engine:如何以编程方式访问我的Model类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文