sql - 如何在 django 视图中设置分页?
问题描述
如何在这个基于函数的视图中设置分页,我尝试了所有默认的 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)
解决方案
作为建议,您可以使用 django-rest-framework 通用视图、分页和 django orm。
分页.py
from rest_framework.pagination import PageNumberPagination
class OrderPagination(PageNumberPagination):
page_size = 20 #default
视图.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()
推荐阅读
- c# - 将二叉树返回到 var
- node.js - 在对象数组中查找 id 字段并返回该对象的另一个字段
- javascript - Vue.js - 重复的事件处理程序和内存使用
- swift - 如何使用 combine Publisher 更改线程?
- dependency-injection - 我可以在 Blazor 客户端应用程序的多个区域中注入 Singleton-DependencyInjected 对象吗?
- python - python中的完全单调插值
- jquery - 在弹出窗口中打开链接
- sql-server - 使用 MS Access 数据库值使 SQL Server 保持最新
- azure - 在 Microsoft WSFed 应用程序登录期间收到 AADSTS700016 错误
- swift - 在这个例子中美元符号有什么作用?