1

I try to use extjs with django, i started extjs with php. for create a paginate grid i used to get total count of the data and get the start and limit value. In django, the pagination does not work. what am i forgot? is it my query? i use postgresql. this is my code. i

    if request.POST['task'] == 'OK':   
        pers = Plante.objects.all().values('id','name','year')
        nbrows = len(pers)

        if request.POST['start']:
            start = request.POST['start']
        else:
            start = request.GET['start']  

        if request.POST['limit']:
            end = request.POST['limit']
        else:
            end = request.GET['limit']


        pers = Plante.objects.all().values('id','name','year')[start:end]     
4

2 回答 2

1
start = int(request.POST.get('start') or request.GET.get('start'))
limit = int(request.POST.get('limit') or request.GET.get('limit'))
pers  = Plante.objects.all().values('id','name','year')[start:start+limit]
于 2013-06-29T15:11:12.130 回答
0

我知道现在已经很晚了,但是这里有一种方法可以使用 EXTJS 发送的“开始”和“限制”分页参数来实现它。

 def fetchRecords(self, params):

    totalCount = 0
    pageNumber = 1
    records = []
    ids = []
    
    #Instanciate your query object
    query = Q()

    #Not really relevant for this case but in case you have any filter criteria params then put them here
    if(params.get("searchStartDate")):
         startDate = datetime.strptime(params.get("searchStartDate"), '%Y-%m-%d').date()
         query &= Q(date_created__gte=startDate)     
    if(params.get("searchEndDate")):
         endDate = datetime.strptime(params.get("searchEndDate"), '%Y-%m-%d').date()
         query &= Q(date_created__lte=endDate)     
    
    # Get the total count, EXT JS Grids need the total count value to be able to paginate
    totalCount = YourModel.objects.filter(query).count()

    #Get the primary keys, we do this because we don't want to get all the objects onto memory. The paginator doesn't 
    #Optimize the fetched data. If your table has millions of records and you load all the record objects to mem, the
    #execution might be quite slow
    your_model_ids_list = YourModel.objects.filter(query).order_by("-id").only('id')

    #Compute the page number based on the pagination "start" & "limit" params sent by EXT grid
    if(int(params.get("start")) != 0 ):
        pageNumber = (int(params.get("start")) / int(params.get("limit"))) + 1

    #Instanciate the paginator object with the unique id's list matching your filter criteria & limit        
    paginator = Paginator(your_model_ids_list, int(params.get("limit")))

    #Get the records that fall on the particular page number that we computed above
    recordIds = paginator.page(pageNumber)

    #Iterate through the record IDs and place them in an array list
    for recordId in recordIds.object_list:
        ids.append(recordId.id)


    #Now fetch the records from your model based on the unique ids that fall on the particular page fetched
    #above
    result = YourModel.objects.filter(Q(pk__in=ids)).order_by("-id")


    #Formulate your response object and return the data
    return {'totalCount': totalCount, 'records': result}
于 2022-02-14T21:28:22.340 回答