Ricardo

DRF 返回全部数据并保留 count 字段

阅读时长: 1 分钟

django restful 风格返回所有数据 并且有相应的字段

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from rest_framework.pagination import BasePagination
from rest_framework.response import Response
from collections import OrderedDict


class TotalPagination(BasePagination):  
    """  
    不分页,显示总数  
    """  
    def paginate_queryset(self, queryset, request, view=None):  
        """  
        返回所有  
        """
        return list(queryset[:])  
  
    def get_paginated_response(self, data):  
        return Response(OrderedDict([  
            ('count', len(data)),  
            ('results', data)  
        ]))

pagination 的 page_size_query_param 参数是怎么获取的? drf 的 get_paginated_responseresponse 格式是怎么来的?

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
记录自己
Built with Hugo
主题 Stack 由 Jimmy 设计