Pagination not working in DRF APIView

后端 未结 4 795
甜味超标
甜味超标 2021-01-20 14:27

I am using APIView for get and post items.
I wanted to implement pagination for my API using Django Rest Framework, but it is not working.

I want

4条回答
  •  悲&欢浪女
    2021-01-20 15:01

    You could make your life a lot easier and just use a ListView. By overriding render_to_response you can make it output json instead of HTML. It does all the pagination for you.

    from django.core import serializers
    from django.views.generic import ListView
    
    class MyListView(JSONResponseMixin, ListView):
        model = models.BaseItem    
        paginate_by = 10  
    
        def render_to_response(self, context):
            return JsonResponse(
                serializers.serialize("json", context),
                **response_kwargs
            )
    

提交回复
热议问题