【问题标题】:Providing arguments to transactions in Datastore Plus (NDB)为 Datastore Plus (NDB) 中的事务提供参数
【发布时间】:2011-04-05 22:45:39
【问题描述】:

在使用 Datastore Plus 时,我无法弄清楚如何将参数传递到事务中。

有人可以重写这个常规数据存储示例代码吗?

from google.appengine.ext import db

class Accumulator(db.Model):
    counter = db.IntegerProperty()

def increment_counter(key, amount):
    obj = db.get(key)
    obj.counter += amount
    obj.put()

q = db.GqlQuery("SELECT * FROM Accumulator")
acc = q.get()

db.run_in_transaction(increment_counter, acc.key(), 5)

我对数据存储区以及最后一行的等价物特别感兴趣。

datastore plus documentation 的示例代码根本不处理参数(在事务中硬编码)。

【问题讨论】:

    标签: python google-app-engine transactions google-cloud-datastore


    【解决方案1】:

    假设您可以按照文档中的示例进行操作,答案是使用 lambda(或命名的辅助函数)。例如

    yield context.transaction(lambda: increment_counter(acc.key(), 5))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      相关资源
      最近更新 更多