How can I implement pagination with python on google appengine? -
i'm building site reddit. objects sorted rating. hoping me implement pagination. want first load top 20 objects. on click of "next" button, want next 20 objects load based on 20th object's rating. here google docs: https://developers.google.com/appengine/docs/python/datastore/queries?hl=en .
does know of way this? or there tutorial out there?
this generic function using in order retrieve model instances along cursor. function takes parameters, reading them request.
def retrieve_dbs(query, order=none, limit=none, cursor=none, **filters): ''' retrieves entities datastore, applying cursor pagination , equality filters. returns dbs , more cursor value ''' limit = limit or config.default_db_limit cursor = cursor.from_websafe_string(cursor) if cursor else none model_class = ndb.model._kind_map[query.kind] if order: o in order.split(','): if o.startswith('-'): query = query.order(-model_class._properties[o[1:]]) else: query = query.order(model_class._properties[o]) prop in filters: if filters.get(prop, none) none: continue if type(filters[prop]) == list: value in filters[prop]: query = query.filter(model_class._properties[prop] == value) else: query = query.filter(model_class._properties[prop] == filters[prop]) model_dbs, more_cursor, more = query.fetch_page(limit, start_cursor=cursor) more_cursor = more_cursor.to_websafe_string() if more else none return list(model_dbs), more_cursor
you can call in fashion. using extentions entity_db , entity_dbs signify variables refer entity object, *_db , *_dbs define if there 1 or many results.
entity_dbs, entity_cursor = retrieve_dbs( model.entity.query(), limit=limit, # limit parameter cursor=cursor, # cursor if want grab batch of next results order=order, )
Comments
Post a Comment