目前就想到了这两种
drf框架返回接口数据的时候返回count接口数据随着数据的增多查询缓慢!
1. 不返回总数据,前端采用下拉刷新方式获取
2. 主键采用int类型,每次返回最后一条数据id数即为总数(first().id - last().id + 1)
from collections import OrderedDict
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.response import Response
class CustomLimitOffsetPagination(LimitOffsetPagination):
"""自定义分页器:优化大数据查询缓慢"""
def get_count(self, queryset):
try:
return queryset.first().id - queryset.last().id + 1
except (AttributeError, TypeError):
return len(queryset)
def get_paginated_response(self, data):
return Response(OrderedDict([('count', self.count), ('results', data)]))