此应用程序用于租赁服务。分发工具时,我需要将客户密钥保存在工具的“who_has”属性中,并将工具密钥添加到分发订单中。如果出现任何问题,我需要删除所有以前编写的 'who_has' 属性。
我认为 Trancastions 是这样做的方法。所以这是我的代码
def get_tools(tool_ids):
tools =[]
for t in tool_ids:
tool = classes.ToolUnits.get_by_id(int(t))
tools.append(tool)
return tools
@ndb.transactional(xg=True)
def process_handout(self):
customer_id = self.request.get("customer")
tool_ids = self.request.get("units")
if not customer_id == 'false' and (not tool_ids=="[]" or not tool_ids):
customer = classes.Customers.get_by_id(int(customer_id))
order = classes.Orders(
customer = customer.key,
handout = True,
transferred = [],
booked = [],
)
tools = get_tools(json.loads(tool_ids))
for tool in tools:
if not tool.who_has:
tool.who_has = customer.key
order.transferred.append(tool.key)
tool.put()
else:
# if tool is already taken
order.booked.append(tool.key)
order.put()
self.redirect('/order/%s' % order.key.id())
else:
self.redirect('/tools')
两个工具都可以正常工作,但是 8 个工具会引发错误:
BadRequestError: operating on too many entity groups in a single transaction.
显然,每个tool.put()
都被视为一个实体组。我如何将他们团结在一个小组中?
我试过文档,但对我来说都是中文的。你能解释一下我的例子吗?