zoukankan      html  css  js  c++  java
  • GenericAPIView, drf内置的基础分页器和偏移分页器的使用, 过滤器, django-filter插件实现区间分类

    GenericAPIView

    '''
    class GenericAPIView(views.APIView):
        ...
        filter_backends = api_settings.DEFAULT_FILTER_BACKENDS
        
        pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
        
    	...
        def filter_queryset(self, queryset):
            for backend in list(self.filter_backends):
                queryset = backend().filter_queryset(self.request, queryset, self)
            return queryset
    
        @property
        def paginator(self):
            if not hasattr(self, '_paginator'):
                if self.pagination_class is None:
                    self._paginator = None
                else:
                    self._paginator = self.pagination_class()
            return self._paginator
    
        def paginate_queryset(self, queryset):
            if self.paginator is None:
                return None
            return self.paginator.paginate_queryset(queryset, self.request, view=self)
    
        def get_paginated_response(self, data):
            assert self.paginator is not None
            return self.paginator.get_paginated_response(data)
    '''
    

    drf内置的基础分页器和偏移分页器的使用

    '''
    # ...luffyapiluffyapiappscourseviews.py
    from rest_framework.viewsets import ReadOnlyModelViewSet
    from . import models, course_serializers, course_pagination
    
    
    class FreeCourseReadOnlyModelViewSet(ReadOnlyModelViewSet):
        ...
        # pagination_class = course_pagination.CourseLimitOffsetPagination
        pagination_class = course_pagination.CoursePageNumberPagination  # 使用分页器后, 前端获取数据的方式为response.data.results
        
    
    # ...luffyapiluffyapiappscoursecourse_pagination.py
    from rest_framework import pagination
    # 基础分页
    class CoursePageNumberPagination(pagination.PageNumberPagination):
        page_size = 10  # 设置一页显示的条数
        page_query_param = 'page'  # 用户可以通过关键字page查询指定页码的数据
        page_size_query_param = 'page_size'  # 用户可以通过关键字page_size对一页显示的条数进行更改
        max_page_size = 10  # 设置一页最大可以显示的条数
    
    
    # 偏移分页
    class CourseLimitOffsetPagination(pagination.LimitOffsetPagination):
        default_limit = 10  # 设置一页显示的条数
        limit_query_param = 'limit'  # 用户可以通过关键字limit对一页显示的条数进行更改
        offset_query_param = 'offset'  # 用户可以通过关键字offset指定一页数据显示的起始位置
        max_limit = 10  # 设置一页最大可以显示的条数
    '''
    

    过滤器

    drf内置搜索过滤器和排序过滤器的使用

    '''
    # ...luffyapiluffyapiappscourseviews.py
    from rest_framework.viewsets import ReadOnlyModelViewSet
    ...
    from rest_framework import filters
    
    
    class FreeCourseReadOnlyModelViewSet(ReadOnlyModelViewSet):
        ...
        filter_backends = [filters.SearchFilter, filters.OrderingFilter]
        
        # 只能使用数据库字段
        search_fields = ['name']  # http://127.0.0.1:8000/course/free/?search=python, 数据量大时需要建index索引
        ordering_fields = ['price', 'id', 'students']  # http://127.0.0.1:8000/course/free/?ordering=-price
    '''
    

    django-filter插件实现区间分类

    安装: pip install django-filter

    '''
    # D:PythonVirtualEnvironmentluffyapiLibsite-packagesdjango_filters
    est_frameworkackends.py
    from . import ..., filterset
    ...
    
    
    class DjangoFilterBackend(metaclass=RenameAttributes):
        filterset_base = filterset.FilterSet
    	
    	...
        def get_filterset_class(self, view, queryset=None):
            ...
            if filterset_class is None and hasattr(view, 'filter_class'):
                ...
                filterset_class = getattr(view, 'filter_class', None)
                
            ...
            if filterset_fields and queryset is not None:
                MetaBase = getattr(self.filterset_base, 'Meta', object)
    
                class AutoFilterSet(self.filterset_base):
                    class Meta(MetaBase):
                        model = queryset.model
                        fields = filterset_fields
    
                return AutoFilterSet
    
            return None
    
    
    # ...luffyapiluffyapiappscourseviews.py
    from rest_framework.viewsets import ReadOnlyModelViewSet
    ...
    from . import models, course_filterset
    from django_filters.rest_framework import DjangoFilterBackend
    
    
    class FreeCourseReadOnlyModelViewSet(ReadOnlyModelViewSet):
        ...
        filter_backends = [DjangoFilterBackend, ]
    
        filter_class = course_filterset.CourseFilterSet
        
    
    # ...luffyapiluffyapiappscoursecourse_filterset.py
    from django_filters.rest_framework.filterset import FilterSet
    from django_filters import filters
    from . import models
    
    
    class CourseFilterSet(FilterSet):
        # 自定义过滤区间
        min_price = filters.NumberFilter(field_name='price', lookup_expr='gte')  # http://127.0.0.1:8000/course/free/?min_price=99
        max_price = filters.NumberFilter(field_name='price', lookup_expr='lte')  # 区间一般为数字区间, 使用NumberFilter类即可
    
        class Meta:
            model = models.Course
            fields = ['course_category']  # 定义分组字段, http://127.0.0.1:8000/course/free/?course_category=1
    '''
    

    自定义过滤器

    '''
    class MyFilter:
        def filter_queryset(self, request, queryset, view):
            ...
                return queryset
    '''
    

    前端视频播放与后端视频托管

    前端详情页视频播放需要先安装插件: cnpm install vue-video-player

    七牛云视频托管

  • 相关阅读:
    Struts2-Spring和Hibernate整合
    windows下使用Eclipse编译执行MapReduce程序 Hadoop2.6.0/Ubuntu
    Android 基于Netty的消息推送方案之对象的传递(四)
    基于JQuery实现表单元素值的回写
    iOS 从UITableViewController中分离数据源
    navicat premium 的使用——navicat 连接MySQL数据库
    【甘道夫】Ubuntu14 server + Hadoop2.2.0环境下Sqoop1.99.3部署记录
    罗永浩和锤子手机:对不起,我被你打动了
    用C语言解决迷宫问题
    Android利用reative_layout生成梅花界面
  • 原文地址:https://www.cnblogs.com/-406454833/p/12709341.html
Copyright © 2011-2022 走看看