class Invoice(ndb.Model): #Child
invoice_id = ndb.StringProperty(required=True) #
class Customer(ndb.Model): #Parent
invoices = ndb.StructuredProperty(Invoices, repeated=True)
def get_invoice_by_id(self, invoice_id):
"""Returns a customer Invoice by invoice_id. Raises KeyError if invoice is not present."""
invoice_matches = [iv for iv in self.invoices if iv.invoice_id == invoice_id]
if not invoice_matches: raise KeyError("Customer has no Invoice with ID %s" % invoice_id)
return invoice_matches[0] # this could be changed to return all matches
在“NoSQL数据存储的最佳实践是什么”的上下文中回答您的问题, 这是我能提供的。在
首先,你可能想用单数来命名你的模型,因为它们应该描述单个的}实体,不是多个。在
Invoice
或{接下来,使用
StructuredProperty
表示您希望将所有这些信息保存在 单一实体-这将减少写/读操作,但可能会引入一些限制。 (见docs- 或者这个related question)最常见的关系是一对多(
Customer
)对多(Invoice
)关系, 其结构如下:请记住此实施的以下限制:
StructuredProperty
s不能在其内部包含重复的属性。在invoice_id
全局唯一性的复杂性将比Invoice
在自己的实体组中更高。(invoice_key.get()
总是比这需要的查询更好)Customer
上的一个实例方法来通过invoice_id
找到{Customer
上存在具有相同ID的发票以下是一些优点:
Customer
invoice_id
查询Invoice
将返回Customer
实例以及所有发票。(事实上,这可能是有利的也可能是不利的——你需要逻辑来从客户那里退回发票)这里有一个比较常见的解决办法,但决不一定是“正确的解决办法” 此解决方案使用祖先关系,允许您保持对
^{pr2}$Invoice
和 相关的Customer
原子-因此您可以在Customer
级别。(total_orders
,total_gross
等)祝阿彭金好运!一旦你掌握了所有这些的窍门,它将是一个真正有价值的平台。在
更新:
这里有一些额外的代码,用于事务性地更新我上面提到的客户合计总数。在
上面的代码在}的子实体。如果该连接丢失,则需要
single entity group transaction
(例如ndb.transactional(xg=False)
)中工作,因为Invoice
是{xg=True
。(我不确定它是否更贵,但优化程度较低)相关问题 更多 >
编程相关推荐