How to jump to specific page using Query Cursor?

后端 未结 1 454
长发绾君心
长发绾君心 2021-01-16 06:51

I am developing my website in python(webapp2) and google data store in back end. I have added query cursor for pagination and it\'s working good but it has only next and pre

1条回答
  •  北海茫月
    2021-01-16 07:39

    You can't. Datastore doesn't know the number of results found by your query.

    You can however use some tricks to simulate a full pagination. For example one technique is to generate a certain number of cursors in a loop and generating the "last page" one.

    Like in the following pseudo-code:

    (results, next_curs, more) = model.query....fetch_page(...)
    
    for p in xrange(5):
        # generate cursor for page number 'b' 
        # by using the next_cursor from previous page
        (results, next_curs, more) = model.....fetch_page(cursor=next_cursor,...)
    

    As you don't access the results cursor the performance is somewhat acceptable (depending on your models complexity, query complexity and such). You can tweak it on your data.

    0 讨论(0)
提交回复
热议问题