''' 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) '''
''' # ...\luffyapi\luffyapi\apps\course\views.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 # ...\luffyapi\luffyapi\apps\course\course_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 # 設置一頁最大能夠顯示的條數 '''
''' # ...\luffyapi\luffyapi\apps\course\views.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 '''
安裝: pip install django-filter前端
''' # D:\PythonVirtualEnvironment\luffyapi\Lib\site-packages\django_filters\rest_framework\backends.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 # ...\luffyapi\luffyapi\apps\course\views.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 # ...\luffyapi\luffyapi\apps\course\course_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-playervue
七牛雲視頻託管python