ndb datastore query fetch_page always returns no cursor and more = false

188 Views Asked by At

I have a class which is an ndb.Model. I am trying to add pagination so I added this:

@classmethod
def get_next_page(cls, cursor):
    q = cls.query()
    q_forward = q.order(cls.title)
    if cursor:
        cursor = ndb.datastore_query.Cursor(cursor)
    objects, cursor, more = q_forward.fetch_page(10, start_cursor=cursor)
    return objects, cursor.urlsafe(), more

However, fetch_page ALWAYS returns more == false and cursor is always just empty. But if I instead of cursor use offset=5 or offset=10 or whatever it works just fine. The cursor does not update so it always starts from the first item.

I am testing this locally with stub context.

I wonder what am I missing? I'm very new to this.

1

There are 1 best solutions below

0
NoCommandLine On
  1. I believe it should be ndb._datastore_query.Cursor (see reference) or just do ndb.Cursor

  2. If the cursor came from UI and you had previously made it urlsafe, then you should be doing ndb._datastore_query.Cursor(urlsafe=cursor) or ndb.Cursor(urlsafe=cursor)

  3. Also, when you don't have a cursor, make sure it's explicitly set to None or just do ndb.Cursor() or ndb._datastore_query.Cursor()