风景是,我需要从数据存储中检索一些数据,模型是:
class Player(ndb.Model):
"""Models an individual Player with key."""
chatId = ndb.StringProperty()
nickname = ndb.StringProperty()
firstName = ndb.StringProperty()
lastName = ndb.StringProperty()
profilePic = ndb.StringProperty()
conversationStatus = ndb.IntegerProperty(default=ConversationStatus.IDLE)
contacts = ndb.KeyProperty(kind="Player", repeated=True)
我想使用“联系人”中的按键来检索玩家。属性。
功能' pageSearchResults'旨在根据页面大小显示有限的结果集。该功能定义如下
prevCursorStr = payload['prev_cursor'] if 'prev_cursor' in payload else ""
nextCursorStr = payload['next_cursor'] if 'next_cursor' in payload else ""
players = []
resultsToFetch = 2
if not prevCursorStr and not nextCursorStr:
players, next_cursor, more = query.order(Player.key).fetch_page(resultsToFetch)
prevCursorStr = ""
if next_cursor:
nextCursorStr = next_cursor.urlsafe()
else:
next_cursor_str = ""
next_ = True if more else False
prev = False
elif nextCursorStr:
cursor = ndb.Cursor(urlsafe=nextCursorStr)
players, next_cursor, more = query.order(Player.key).fetch_page(resultsToFetch, start_cursor=cursor)
prevCursorStr = nextCursorStr
nextCursorStr = next_cursor.urlsafe()
prev = True
next_ = True if more else False
elif prevCursorStr:
cursor = ndb.Cursor(urlsafe=prevCursorStr)
players, next_cursor, more = query.order(-Player.key).fetch_page(resultsToFetch, start_cursor=cursor)
players.reverse()
nextCursorStr = prevCursorStr
prevCursorStr = next_cursor.urlsafe()
prev = True if more else False
next_ = True
result = {
'players': players,
'search': payload['search'] if 'search' in payload else "",
'next_cursor': nextCursorStr if next_ else "",
'prev_cursor': prevCursorStr if prev else "",
}
return result
我们假设我做了以下事情:
keys = player.contacts
query = Player.query(Player.key.IN(keys))
return self.pageSearchResults(query, payload)
在这种情况下,我有一个奇怪的问题,分页按顺序正常工作,它给我回到结果列表,如: 1,2, - >下一页 < - prev page 3
如果我点击上一页,相同的功能会让我回来 3 - >下一页 这是不正确的。
现在,如果我查询的属性不是KeyProperty,问题就会消失。
出于调试目的,我将此代码添加到上一个代码段
keys = player.contacts
chatids = []
for key in keys:
player = key.get()
chatids.append(player.chatId)
query = Player.query(Player.chatId.IN(chatIds))
return self.pageSearchResults(query, payload)
在这种情况下,我查询chatId StringProperty,分页就像魅力一样。
我真的不明白这里发生了什么,有人可以帮助我吗?
答案 0 :(得分:0)
模型的关键属性应按以下方式过滤:
keys = player.contacts
query = Player.query(Player._key.IN(keys))
return self.pageSearchResults(query, payload)
请注意_key
。对于与密钥匹配的联系人:
query = Player.query(Player.contacts.IN(keys))