django - 分页 DRF 无法正常工作
问题描述
当我在我的项目中放置默认分页时,我的分页有点问题,某些页面在其他页面中工作,例如:
这是我所有项目的文件 settings.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'apicolonybit.notification_clbt.NotificationPagination.PaginationList'}
这是我项目中的配置应用程序:myproject / Configuration
class ConfigurationsList(generics.ListAPIView):
"""
list configuration with current user authenticated.
"""
queryset = Configuration.objects.all()
serializer_class = ConfigurationSerializer
当我在邮递员中运行这部分时,运行良好,但是当我尝试在另一个模块中运行时:
class TransactionListHistory(generics.ListAPIView):
# queryset = TransactionHistory.objects.all()
serializer_class = TransactionHistorySerializer
pagination_class = PaginationList
page_size = 2
page = 1
def get_object(self, current_user):
# User.objects.get(id=pk)
return TransactionHistory.objects.filter(agent_id=current_user.id).order_by('-id')
@classmethod
def get_object_client(cls, current_user, trans):
# User.objects.get(id=pk)
return TransactionHistory.objects.filter(user_id=current_user.id).order_by('-id')
def get(self, request, format=None):
current_user = request.user
status_trans = 6
agent_user = 2
client_user = 1
trans = {
'status': status_trans
}
typeusers = Profile.objects.get(user_id=current_user.id)
# actions agent user = show all transaction from all client users
if typeusers.type_user == agent_user:
list_trans_init = self.get_object(current_user)
serializer = TransactionHistorySerializer(list_trans_init, many=True)
get_data = serializer.data
# actions normal user (client user) = just see transactions from self user
if typeusers.type_user == client_user:
list_trans_init = self.get_object_client(current_user, trans)
serializer = TransactionHistorySerializer(list_trans_init, many=True)
get_data = serializer.data
# if not PaginationList.get_next_link(self):
# return JsonResponse({'data': get_data}, safe=False, status=status.HTTP_200_OK)
return self.get_paginated_response(get_data)
我的自定义文件分页是这样的
class PaginationList(PageNumberPagination):
page_size = 2 # when show me an error I added
offset = 1 # when show me an error I added
limit = 10 # when show me an error I added
count = 10 # when show me an error I added
page = 1 # when show me an error I added
def get_paginated_response(self, data):
return Response({
'links': {
'next': self.get_next_link(),
'previous': self.get_previous_link()
},
'count': self.page.paginator.count,
'results': data
})
变量 page_size 等,然后向我显示一个错误,例如 PaginationList is not object page,我添加了这个 page_size 并传递了其他错误,例如 PaginationList is not object offset 并再次添加了 var。
好吧,最后一个错误告诉我就像这个'int'对象没有属性'has_next'
请帮助我,如何在我的类 TransactionListHistory 中添加我的自定义分页
感谢您的关注。
解决方案
你使用self.get_paginated_response()
错误的方式。
from rest_framework.response import Response
class TransactionListHistory(generics.ListAPIView):
# Your code
def get(self, request, *args, **kwargs):
queryset = do_something_and_return_QuerySet() # do some logical things here and
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
这do_something_and_return_QuerySet()
是你的功能或逻辑,它返回 a QuerySet
。
例子
class TransactionListHistory(generics.ListAPIView):
# Your code
def get(self, request, *args, **kwargs):
queryset = TransactionHistory.objects.filter(user_id=request.user.id)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
推荐阅读
- r - Gurobi 9.1.2 的 R 安装不适用于 R 4.0.4
- angular - 如何在没有角度应用程序 src 文件夹的情况下运行角度量角器测试(e2e)。是否可以独立运行角度单元测试,这意味着没有devsrc?
- nexmo - 如何在nexmo客户端js中监听会话:成功事件?
- powershell - PowerShell 中的 OpenFileDialog - 可以更改对话框的图标吗?
- python - Pytorch 中输入的期望值是多少?
- python - 创建乘法表
- ios - 如何从 Windows 在 iOS 模拟器上启动 xamarin 应用程序?
- scala - Spark Scala:找不到数据源:kafka
- python - Python 列表索引
- c# - C# ElasticSearch 使用分析器进行查询