配置文件中添加
INSTALLED_APPS = [
…
‘django_filters’,
…
]
REST_FRAMEWORK = {
‘DEFAULT_FILTER_BACKENDS’: [‘django_filters.rest_framework.DjangoFilterBackend’]
}
在视图中
from django_filters import rest_framework as filters
from rest_framework.filters import OrderingFilter
在视图类中
filter 操作依赖于filter_backends,所以需要先指定backends
filter_backends = (filters.DjangoFilterBackend, OrderingFilter)
#指定filter 的字段
ordering_fields = (“update_time”, “browse_count”)
倒序排序在前加-即可
示例代码:
from django.db.models import Count, Sum
from django_filters import rest_framework as filters
from drf_yasg.utils import swagger_auto_schema
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
from bmiss_knowledge_base.filter import KnowledgeBaseFilterSet
from bmiss_knowledge_base.models import Knowledge
from bmiss_knowledge_base.prefetchers import KnowledgeIndexTopDataFetcher
from bmiss_knowledge_base.serializers import (
KnowledgeBaseSerializer,
KnowledgeIndexStatisticsSerializer,
KnowledgeIndexTopSerializer,
KnowledgeTopSerializer,
)
class KnowledgeIndexViewSet(GenericViewSet):
"""
知识库首页(知识库统计数据接口,知识库每种技术类型最高访问量数据接口,最新动态的前三十条数据接口)
"""
queryset = Knowledge.objects.filter().order_by("-update_time")
serializer_class = KnowledgeBaseSerializer
filterset_class = KnowledgeBaseFilterSet
filter_backends = (filters.DjangoFilterBackend, OrderingFilter)
ordering_fields = ("update_time", "browse_count")
@swagger_auto_schema(
operation_summary="知识库首页统计数据接口",
responses={
"200": KnowledgeIndexStatisticsSerializer(),
},
operation_description="统计数据",
)
@action(methods=["get"], detail=False, url_path="overview-statistics")
def get_knowledge_base_statistics(self, request):
"""
统计数据
返回知识库数据总数,编辑次数,知识库总人数,以及倒叙的前6个知识库数据
"""
knowledge_base_count = Knowledge.objects.filter().count()
edit_count = Knowledge.objects.aggregate(nums=Sum("edit_count"))
person_count = Knowledge.objects.values("create_user_id").annotate(
nums=Count("create_user_id")
)
knowledge_queryset = Knowledge.objects.filter().order_by("-create_time")[:6]
serializer = KnowledgeBaseSerializer(knowledge_queryset, many=True)
res = {
"knowledge_base_count": knowledge_base_count,
"edit_count": edit_count["nums"],
"person_count": len(person_count),
"knowledge_queryset": serializer.data,
}
return Response(res)
@swagger_auto_schema(
operation_summary="知识库的根据ordering_fields排序后的前N条数据",
responses={
"200": KnowledgeIndexTopSerializer(),
},
operation_description="知识库的根据ordering_fields排序后的前N条数据",
)
@action(methods=["get"], detail=False, url_path="top")
def get_knowledge_index_top(self, request):
"""
知识库的根据ordering_fields排序后的前N条数据
ordering_fields:-browse_count 根据浏览量倒序排序
-create_time 根据时间倒序排序
"""
k_ser = KnowledgeTopSerializer(data=request.query_params)
k_ser.is_valid()
N = int(k_ser.validated_data["N"])
queryset = self.filter_queryset(self.get_queryset())[:N]
knowledge_obj_list = KnowledgeIndexTopDataFetcher(queryset, request).data
return Response(knowledge_obj_list)