This is brett's Typepad Profile.
Join Typepad and start following brett's activity
brett
Recent Activity
Use a piece of this to define sync() for yourself, and you should be good to go. Just make sure to do sync() after the transaction finishes (using a closure, or something):
class MyModel(db.Model):
foo = db.IntegerProperty(default=0)
def add_one(self):
python_scoping_is_annoying = [None]
def txn():
new = python_scoping_is_annoying[0] = db.get(self.key())
new.foo += 1
new.put()
db.run_in_transaction(txn)
new = python_scoping_is_annoying[0]
# Copy all new values over to this instance.
for k in new.properties().keys() + new.dynamic_properties():
setattr(self, k, getattr(new, k))
a = MyModel()
a.put()
print a.key(), a.foo
a.add_one()
print a.key(), a.foo
a.add_one()
print a.key(), a.foo
Output:
agVzaGVsbHIPCxIHTXlNb2RlbBjKqwUM 0
agVzaGVsbHIPCxIHTXlNb2RlbBjKqwUM 1
agVzaGVsbHIPCxIHTXlNb2RlbBjKqwUM 2
Google App Engine: Data Duplicity
I've stumbled across a real irritation with the way the Google App Engine data model works. One among many frankly, but I'll restrict myself to just this one for now. The issue stems from the fact that db.run_in_transaction(func) insists that func is a function with no side effects, since it m...
Subscribe to brett’s Recent Activity