drf 프레임워크 사용자 정의 페이지

15988 단어 Django
drf 프레임워크 사용자 정의 페이지
4
  • PageNumberPagination 다시 쓰기 4
  • from rest_framework.pagination import PageNumberPagination
    from collections import OrderedDict
    from rest_framework.response import Response
    
    
    class StandardResultsSetPagination(PageNumberPagination):
        """      ,   list     """
    
        page_size = 10  #        
        page_size_query_param = 'page_size'  #            
        max_page_size = 20  #          
    
        def get_paginated_response(self, data):
            return Response(OrderedDict([
                ('count', self.page.paginator.count),
                ('next', self.get_next_link()),
                ('previous', self.get_previous_link()),
                ('code', 0),
                ('message', 'Ok'),
                ('results', data),
            ]))
    

    4
  • 보기에서 어떻게 사용하는지 4
  •     def list(self, request, *args, **kwargs):
            """            """
            recommend_model_id = request.query_params.get('recommend_model_id')
            #   
            queryset = RecommendFabric.objects.filter(recommend_model_id=recommend_model_id).select_related(
                'fabrics', 'recommend_model').order_by('-sequence', '-id')
    
            #   
            page = self.paginate_queryset(queryset)
            if page is not None:
                serializer = self.get_serializer(page, many=True)
                data = serializer.data
    
                data = {"data": data}
                return self.get_paginated_response(data=data)
    
            #    
            serializer = self.get_serializer(queryset, many=True)
            data = serializer.data
    
            data = {"data": data}
            return APIResponse.success(data=data)
    

    4
  • setting의 설정
    REST_FRAMEWORK = {
        #     
        # 'EXCEPTION_HANDLER': 'one_fashion.utils.exceptions.exception_handler',
        #   
        'DEFAULT_AUTHENTICATION_CLASSES': (
                'rest_framework_jwt.authentication.JSONWebTokenAuthentication',  #   JWT  
                'rest_framework.authentication.SessionAuthentication',
                'rest_framework.authentication.BasicAuthentication',
            ),
        #     
        'DEFAULT_PAGINATION_CLASS': 'one_fashion.utils.pagination.StandardResultsSetPagination',
        'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',
        #     (  )
        # 'DEFAULT_PAGINATION_CLASS': 'one_fashion.apps.management.page.CarPageNumberPagination',
        # 'PAGE_SIZE': 10,
        #     
        'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
    }
    
    

    4
  • 페이지 결과 4
  • {
        "count": 3,
        "next": null,
        "previous": null,
        "code": 0,
        "message": "Ok",
        "results": {
            "data": [
                {
                    "recommend_model_id": 12,
                    "model_name": "   ",
                    "more_name": "   ",
                    "create_time": "2020-05-29T16:21:44",
                    "category_name": "     ",
                    "url": "",
                    "category": 2
                },
                {
                    "recommend_model_id": 11,
                    "model_name": "  ",
                    "more_name": "  ",
                    "create_time": "2020-05-29T16:21:31",
                    "category_name": "     ",
                    "url": "",
                    "category": 2
                },
                {
                    "recommend_model_id": 3,
                    "model_name": "     ",
                    "more_name": "  ",
                    "create_time": "2020-04-30T12:01:10",
                    "category_name": "     ",
                    "url": "",
                    "category": 2
                }
            ]
        }
    }
    

    좋은 웹페이지 즐겨찾기