下面是python代码,我在其中尝试从预订模型获取预订信息。在
i=0
for c in courts:
court = names[i]
i=i+1
c_key=c.key()
logging.info("c_key: %s " % c_key)
weekday_key= db.Key.from_path('Courts', 'c_key', 'Days', weekday)
logging.info("weekday_key: %s " % weekday_key)
logging.info("weekday: %s " % weekday)
logging.info("court: %s " % court)
reservation = db.Query(Reservations)
nlimit=2*len(times)
reservations = reservation.fetch(limit=nlimit)
logging.info("reservations: %s " % len(reservations))
我的法院数据库中只有两个法院实体,court1和court2。
在MyDays数据库中也只有14个工作日实体,其中7个用于court1,7个用于court2,命名为Sunday。。。,星期六。在当前的示例中,我尝试获取两个星期一的密钥,一个用于court1,一个用于court2。
我不明白为什么根据下面的日志,我得到相同的weekday_key
,这两个不同的法院有不同的密钥c_key
。在
在下面的日志中,无论我在db.Key.from_path(
命令中输入'c\u key'还是'court',我得到的结果完全相同,这表明2weekday_key
的值是相同的,而不是我预期的不同。在
我的模型如下。在
class Courts(db.Model): #parent is Locations, courtname is key_name
location = db.ReferenceProperty(Locations)
timezone = db.StringProperty()
class Days (db.Model): #parent is Courts, name is key_name, day of week
court = db.ReferenceProperty(Courts)
startTime = db.ListProperty(int)
endTime = db.ListProperty(int)
class Reservations(db.Model): #parent is Days, hour, minute HH:MM is key_name
weekday = db.ReferenceProperty(Days)
day = db.IntegerProperty()
nowweekday = db.IntegerProperty()
name = db.StringProperty()
hour = db.IntegerProperty()
minute = db.IntegerProperty()
您知道键的值,所以您可以手工创建一个键(可以这么说),然后使用该键作为祖先进行查询。在
例如:
但在这里你可以用类似
^{pr2}$以此类推,这样你就可以沿着链条往下走,用你掌握的所有信息(即他们想保留哪家法庭)来构建密钥。 然后祖先查询的结果将是那些拥有您作为其祖先传递的键的模型。在
https://developers.google.com/appengine/docs/python/ndb/queries#ancestor
我不喜欢这个答案的地方是
weekday_key
在法庭上对所有c都是一样的。这似乎不对。在每次都是使用字符串
'c_key'
计算键,而不是变量c_key
的值。在然而,即使您修复了这个问题,它仍然无法工作,因为您需要的是法院的ID,而不是完整的密钥路径。在
相关问题 更多 >
编程相关推荐