Python 在Google应用程序引擎上批量获取子实体?
在Python中的Google App Engine上,我有一个访问实体,它有一个Patient的父对象。患者可能有多次就诊 我需要在某个地方设置最近的访问(以及一些辅助访问数据),以便稍后查询,可能在Brett Slatkin可能称为“关系索引实体”的另一个子实体中 我希望以批量方式这样做,如下所示:Python 在Google应用程序引擎上批量获取子实体?,python,google-app-engine,google-cloud-datastore,Python,Google App Engine,Google Cloud Datastore,在Python中的Google App Engine上,我有一个访问实体,它有一个Patient的父对象。患者可能有多次就诊 我需要在某个地方设置最近的访问(以及一些辅助访问数据),以便稍后查询,可能在Brett Slatkin可能称为“关系索引实体”的另一个子实体中 我希望以批量方式这样做,如下所示: 1. get 100 Patient keys 2. get all Visits that have any of the Patients from 1 as an ancestor 3.
1. get 100 Patient keys
2. get all Visits that have any of the Patients from 1 as an ancestor
3. go through the Visits and get the latest for each Patient
有没有办法在批量获取中执行步骤2
我知道有一种方法可以从键列表中批量获取实体,也有一种方法可以通过其祖先匹配单个实体。在数据存储中无法找到您要查找的查询类型;它是不受支持的联接。第2步将需要100个查询
您可以在每个患者实体中存储就诊密钥列表,然后可以根据这些实体的密钥执行简单的批量获取操作。为什么不在患者对象上维护患者就诊密钥列表?您将已经知道最后一次访问,因为它将是添加到列表中的最后一个键
我的NF探测器发疯了,这让我认为这是数据存储的正确解决方案。如果您在模型
就诊
中设置属性患者
,如下所示:
class Patient(db.Model):
pass
class Visit(db.Model):
patient_visited = db.ReferenceProperty(Patient)
for patient in Patient.all():
last_visit = patient.visit_set.order('-visited')
每个患者实体上都会有此自动查询属性就诊集
,因此您可以这样查询:
class Patient(db.Model):
pass
class Visit(db.Model):
patient_visited = db.ReferenceProperty(Patient)
for patient in Patient.all():
last_visit = patient.visit_set.order('-visited')
一切都完成了。
只需101次查询。很好