4

I'm trying to fetch results in a python2.7 appengine app using cursors, but each time I use with_cursor() it fetches the same result set.

query = Model.all().filter("profile =", p_key).order('-created')

    if r.get('cursor'):
        query = query.with_cursor(start_cursor = r.get('cursor'))
        cursor = query.cursor()

    objs = query.fetch(limit=10)     
    count = len(objs)

    for obj in objs:
        ...

Each time through I'm getting same 10 results. I'm thinkng it has to do with using end_cursor, but how do I get that value if query.cursor() is returning the start_cursor. I've looked through the docs but this is poorly documented.

4

2 回答 2

4

顺便说一句,您的格式有点古怪。查看您的代码(这是不完整的,因此可能会遗漏一些内容。)我不得不假设您在获取结果后忘记存储光标(或返回给用户 - 我假设 r 是一个请求?)。

因此,在您获取一些数据后,您需要在查询中调用 cursor()。例如,此函数使用游标计算所有实体。

def count_entities(kind):
    c = None
    count = 0
    q = kind.all(keys_only=True)
    while True:

        if c:
            q.with_cursor(c)
        i = q.fetch(1000)
        count = count + len(i)
        if not i:
            break
        c = q.cursor()
    return count

看看在调用 fetch() 之后是如何c=q.cursor()调用的,它在下一次循环中被用作光标。

于 2012-08-23T23:41:05.877 回答
2

这是最终奏效的方法:

query = Model.all().filter("profile =", p_key).order('-created')

if request.get('cursor'):
    query = query.with_cursor(request.get('cursor'))

objs = query.fetch(limit=10) 
cursor = query.cursor()                

for obj in objs:
    ...
于 2012-08-24T00:13:09.973 回答