寻找一种有效的方法来检查db.Model
的特定属性是否已更改,而putting
之前是否存储在数据存储中的版本。显然我想避免从数据库中获取新的实例!可能吗?像这样:
class MyClass(db.Model):
prop = db.StringProperty()
instance = MyClass()
instance.string_property = 'string'
instance.put()
instance2 = db.get(instance.key())
instance2.string_property = 'string2'
instance2.put() #would like to detect that string_property was altered!
答案 0 :(得分:2)
我认为这是一个坏主意。
如果你看一下db的代码(你现在应该真正使用ndb),每个属性/模型都会有很多内容(这里使用了元类)并且取决于类/属性和/或分配的值你可能会开始得到一些真正奇怪的行为和/或错误。
这并不意味着你应该,但要仔细考虑你在做什么;-)
我会考虑使用hooks(se nick johnsons blog for blog ideas.notdot.net/2010/04 / ...)或切换到ndb并使用它的pre / post钩子(参见模型钩子https://developers.google.com/appengine/docs/python/ndb/entities#hooks)
答案 1 :(得分:0)
我意识到这只是一个基本的Python编程问题:
class MyClass():
prop = 'init string'
def __setattr__(self, name, value):
print name + ' is being updated to ' + value
test = MyClass()
test.prop = 'summing else'
prop is being updated to summing else