How to show total pages in JSON response in Django Pagination?

旧时模样 提交于 2021-02-19 07:58:06

问题


I am currently working on pagination in Django restful framework. I am successfully done with pagination. but the problem I am facing is that "JSON response does not include information about total pages in my query and other information like total records etc". how can I include this information in my response. my view.py is

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
#######################View all mobiles
@api_view(['GET'])
def getAll_Mobiles(request):
    try:
        Mobile_all = Mobile.objects.all()
        paginator = Paginator(Mobile_all, 10)

        page = request.GET.get('page')
        try:
            users = paginator.page(page)
        except PageNotAnInteger:
            users = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999),
            # deliver last page of results.
            users = paginator.page(paginator.num_pages)
        serializer_context = {'request': request}
        serializer = Mobile_Serializer(users,many=True,context=serializer_context)
        return Response(serializer.data)
    except Mobile.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

and my API returns record on changing page in URL. but it does not give me response information. Can anybody please tell me how to include this information in response. I will be very thankful for this favour.


回答1:


       if serializer.data["hasPagination"]:
            paginator = Paginator(result_serializer.data, settings.PAGE_SIZE)
            page = request.GET.get('page', 1)
            result = paginator.get_page(page)
            return Response(data={
                'results': result.object_list,
                'total_records': paginator.count,
                'total_pages': paginator.num_pages,
                'page': result.number,
                'has_next': result.has_next(),
                'has_prev': result.has_previous()
            }, status=status.HTTP_200_OK)



回答2:


You should be able to access the number of pages with self.page.paginator.num_pages inside a serializer, at it's an attribute of the paginator.




回答3:


You can take a look how this is done by DRF itself.

In your example, the implementation may look like this:

#users is a Page object returned from the django paginator class
#its not a list of users, it just act as a list of users when iterated
return Response({
    'count': paginator.count,
    'num_pages': paginator.num_pages,
    'results': serializer.data
})



回答4:


A simple way is :

# instead of returning serializer data instantly:
# return Response(serializer.data)
res = {'data': serializer.data, 'pages_count': paginator.num_pages()}
return Response(res)

Another way is overwrite to_represntation method in your serializer...



来源:https://stackoverflow.com/questions/44343425/how-to-show-total-pages-in-json-response-in-django-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!