[Django] Pagination | DRF pagination

The Pagination that comes with Django is very useful to me personally, you can directly refer to the official website document, Pagination , I personally think it is very clear

As for the pagination of DRF, it seems that some of its rules need to be followed to use it, and I am useless. But record the learning materials in the search:
Django-drf-the use of the built-in pager : which Ⅴ 继承APIView的分页方式is more suitable for my
DRF pagination
. If you use the DRF method and want to get some parameters in the Pagination instance that comes with Django, suppose DRF The instance of PageNumberPagination is p_obj, p_obj.page.paginatorwhich is the Pagination instance, followed by the specific parameters of Pagination ( django official document Paginator )

Combined use of DRF's pagination + serializer

from rest_framework.pagination import PageNumberPagination

# 自定义了一个类,可以让前端传入page_size,不同写死一页有多少数据
class BasePageNumberPagination(PageNumberPagination)
	def __init__(self, page_size):
		self.page_size = page_size

# View中调用代码
p_obj = BasePageNumberPagination(page_size)
# request除page_size参数外还需定义一个page参数,表明当前数据是第几页
obj_list = p_obj.paginate_queryset(obj_list, request, self)	
ser = ObjSerializer(obj_list, many=True)
# 给前端返回ser.data

Guess you like

Origin blog.csdn.net/qq_42438771/article/details/119755078