用光标向后分页正在工作,但缺少一个项目 [英] Backward pagination with cursor is working but missing an item
问题描述
从寻找提供页面/项目计数/导航符合GAE数据存储查询的项目的想法/替代方法,我可以找到提示如何通过REVERSING ORDER向后翻页使用单个光标。
class CursorTests(test_utils.NDBTest):
def testFirst(self):
class Bar(model.Model):
value = model.IntegerProperty()
self.entities = []
在范围内(10):
e = Bar(value = i)
e.put()
self.entities.append(e)
q = Bar.query()
bars,next_cursor,more = q.order(Bar.key ).fetch_page(3)
barz,another_cursor,more2 = q.order(-Bar.key).fetch_page(3,start_cursor = next_cursor)
self.assertEqual(len(bars),len(barz ))
不幸的是,这个错误失败了。
$ b
追溯(最近最后一次调用最后):文件
/ blockquote>
/ Users / reiot / Documents / Works / appengine-ndb-experiment
self.assertEqual(len(bars),len(baz))AssertionError:3!= 2
是的,边界中的项目缺少反向查询。
bars = [Bar(key = Key('Bar',1),value = 0),Bar(key = Key ('Bar',2),value = 1),Bar(key = Key('Bar',3),value = 2)]
bars = [Bar(key = Key('Bar',2) ,value = 1),Bar(key = Key('Bar',1),value = 0)]
我怎样才能解决这个问题?
解决方案
好的,这是官方的答案。您需要反转游标,如下所示:
rev_cursor = cursor.reversed()
我自己并不知道这件事。 :-(我会确保它显示在fetch_page()的文档中。
From looking for ideas/alternatives to providing a page/item count/navigation of items matching a GAE datastore query, I could find a hint how to backward page navigation with a single cursor by REVERSING ORDER.
class CursorTests(test_utils.NDBTest):
def testFirst(self):
class Bar(model.Model):
value = model.IntegerProperty()
self.entities = []
for i in range(10):
e = Bar(value=i)
e.put()
self.entities.append(e)
q = Bar.query()
bars, next_cursor, more = q.order(Bar.key).fetch_page(3)
barz, another_cursor, more2 = q.order(-Bar.key).fetch_page(3, start_cursor=next_cursor)
self.assertEqual(len(bars), len(barz))
Unfortunately it failed with this error.
Traceback (most recent call last): File "/Users/reiot/Documents/Works/appengine-ndb-experiment/ndb/query_test.py", line 32, in testFirst self.assertEqual(len(bars), len(baz)) AssertionError: 3 != 2
Yes, an item in boundary is missing with reverse query.
bars = [Bar(key=Key('Bar', 1), value=0), Bar(key=Key('Bar', 2), value=1), Bar(key=Key('Bar', 3), value=2)]
bars = [Bar(key=Key('Bar', 2), value=1), Bar(key=Key('Bar', 1), value=0)]
How can I fix this problem?
Ok, here's the official answer. You need to "reverse" the cursor, as follows:
rev_cursor = cursor.reversed()
I did not know this myself. :-( I'll make sure this is shown in the docs for fetch_page().
这篇关于用光标向后分页正在工作,但缺少一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!