根据祖父母在数据存储中搜索数据

2024-05-16 00:29:51 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是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的值是相同的,而不是我预期的不同。在

^{pr2}$

我的模型如下。在

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()

Tags: keynameinfodbisloggingdaysreservations
3条回答

How do I construct a query for all Reservations on a specific day in Days at a specific court in Courts on a specific weekday in (week)Days?

您知道键的值,所以您可以手工创建一个键(可以这么说),然后使用该键作为祖先进行查询。在

例如:

key = ndb.Key(BlogPost, 12345)
qry = Comment.query(ancestor=key)

但在这里你可以用类似

^{pr2}$

以此类推,这样你就可以沿着链条往下走,用你掌握的所有信息(即他们想保留哪家法庭)来构建密钥。 然后祖先查询的结果将是那些拥有您作为其祖先传递的键的模型。在

https://developers.google.com/appengine/docs/python/ndb/queries#ancestor

    i=0
    for c in courts:
        court_id = names[i]
        i=i+1
        weekday_key = db.Key.from_path('Courts', c.key().name(), 'Days', weekday)
        reservation=Reservations.all()
        reservation.ancestor(weekday_key)
        nlimit=2*len(times)
        reservations = reservation.fetch(limit=nlimit)

我不喜欢这个答案的地方是weekday_key在法庭上对所有c都是一样的。这似乎不对。在

每次都是使用字符串'c_key'计算键,而不是变量c_key的值。在

然而,即使您修复了这个问题,它仍然无法工作,因为您需要的是法院的ID,而不是完整的密钥路径。在

相关问题 更多 >