当我在项目中放入默认分页时,我对分页有一点问题,例如某些页面在其他页面上不起作用,例如:
这是我所有项目的文件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不是对象页面,我添加了此page_size并传递了其他错误,例如PaginationList不是对象偏移,然后再次添加了var。
最后一个错误告诉我,这个'int'对象没有属性'has_next'
请帮助我,如何在我的类TransactionListHistory中添加自定义分页
感谢您的关注。
答案 0 :(得分:0)
您使用 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()
是您的功能或逻辑,它们返回 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)