2013-04-05 3 views
2

나는 reddit과 같은 사이트를 만들고 있습니다. 객체는 등급순으로 정렬됩니다. 누군가가 페이지 매김을 구현하는 데 도움이되기를 바랍니다. 난 그냥 먼저 상위 20 개체를로드하고 싶습니다. 그런 다음 "다음"버튼을 클릭하면 20 번째 객체의 등급에 따라 다음 20 개의 객체가로드되기를 원합니다. 다음은 Google 문서입니다 : https://developers.google.com/appengine/docs/python/datastore/queries?hl=en.Google appengine에서 파이썬으로 페이지 매김을 구현하려면 어떻게해야합니까?

누구든지이 작업을 수행 할 수있는 방법을 알고 있습니까? 아니면 거기에 좋은 지침서가 있습니까? 당신이 당신의 데이터 저장소 쿼리에 제한 = 20을 사용하고, 얻을 나중에 커서를 얻을 경우

답변

1

이것은 매우 간단하다 이것은 내가 내 모델 인스턴스를 검색하기 위해 사용하고 일반적인 기능입니다 (20)

+0

어떻게 "커서를 나중에 가져 옵니까?" 이 답변을 보았습니다 : http : //stackoverflow.com/questions/4840731/how-to-use-cursor-for-pagination? rq = 1 그러나 무슨 일이 일어나고 있는지 잘 모르겠습니다. – tjones

+1

query.get()을 호출 한 후 query.cursor()를 호출하여 커서를 가져옵니다. 다음 버튼을 클릭 할 때 다음 쿼리를 실행하면 다음 쿼리에서 커서를 전달합니다. docs : https://developers.google.com/appengine/docs/python/datastore/queryclass#Query_run을 참조하십시오. – dragonx

2

다음 커서와 함께. 이 함수는 요청에서 매개 변수를 읽는 매개 변수를 사용합니다.

def retrieve_dbs(query, order=None, limit=None, cursor=None, **filters): 
    ''' Retrieves entities from datastore, by applying cursor pagination 
    and equality filters. Returns dbs and 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: 
    for o in order.split(','): 
     if o.startswith('-'): 
     query = query.order(-model_class._properties[o[1:]]) 
     else: 
     query = query.order(model_class._properties[o]) 

    for prop in filters: 
    if filters.get(prop, None) is None: 
     continue 
    if type(filters[prop]) == list: 
     for 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 

그런 방식으로 호출 할 수 있습니다. entity_db 및 entity_dbs 확장을 사용하여 내 변수가 엔티티 객체를 참조 함을 나타냅니다. * _db 및 * _dbs는 하나 이상의 결과가있을 경우 정의합니다.

entity_dbs, entity_cursor = retrieve_dbs(
    model.Entity.query(), 
    limit=limit, # Your limit parameter 
    cursor=cursor, # The cursor if you want to grab a batch of next results 
    order=order, 
) 
관련 문제