【问题标题】:how to set pagination in django views?如何在 django 视图中设置分页?
【发布时间】:2020-06-18 02:05:33
【问题描述】:

如何在这个基于函数的视图中设置分页,我尝试了所有默认的 django_pagination 但我没有得到任何帮助。

class Order_ListAPIView(APIView):

   def get(self,request,format=None):

       if request.method == 'GET':
           cur,conn = connection()
           order_query = ''' SELECT * FROM orders'''
           order_detail_query = ''' SELECT * FROM order_details'''

           with conn.cursor(MySQLdb.cursors.DictCursor) as cursor:

               cursor.execute(order_query)
               order_result = cursor.fetchall()
               order_data = list(order_result)

           ...
            ... #rest_code
             ...


           return Response({"order_data":order_data},status=status.HTTP_200_OK)
       else:
           return Response(status=status.HTTP_400_BAD_REQUEST)

【问题讨论】:

    标签: sql django django-rest-framework pagination


    【解决方案1】:

    作为建议,您可以使用 django-rest-framework generic viewspaginations 和 django orm

    分页.py

    from rest_framework.pagination import PageNumberPagination
    class OrderPagination(PageNumberPagination):
        page_size = 20 #default
    

    views.py

    from rest_framework.generics import ListAPIView
    from .paginations import OrderPagination
    from .models import Order
    
    class OrderListAPIView(ListAPIView):
       serializer_class = your_serializer_class
       pagination_class = OrderPagination
       queryset = Order.objects.all()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 2019-07-01
      • 2012-08-01
      • 2017-03-04
      • 1970-01-01
      相关资源
      最近更新 更多