3

寻找一种有效的方法来检查 a 的特定属性是否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!
4

2 回答 2

2

我认为那是个坏主意。

如果您查看 db 的代码(您现在应该真正使用 ndb),那么每个属性/模型(元类在这里大量使用)的引擎盖下都会发生很多事情,并且取决于类/属性和/或值被分配后,您可能会开始遇到一些真正奇怪的行为和/或错误。

这并不意味着你应该,但要仔细考虑你在做什么;-)

我会考虑使用钩子(se nick johnsons blog 获取一些想法 blog.notdot.net/2010/04/...)或切换到 ndb 并使用它的 pre/post 钩子(参见模型钩子 https://developers.google.com /appengine/docs/python/ndb/entities#hooks )

于 2013-04-03T12:05:11.150 回答
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
于 2013-04-03T10:33:14.927 回答