1.自定义继承了ModelViewSet视图集的类视图中,默认只有list,create,retrieve,update,delete这几类action方法,我们可以自定义action方法,来获取我们指定返回的数据,而不用改变ModelSerializer序列化器.
1.1 自定义names这个action来获取项目id和项目名
1.2 自定义的names方法,一定要用@action(methods=["GET"], detail=False)装饰器装饰才能
在路由表中进行“get“:“names”路径映射
import json
from django.http import HttpResponse, JsonResponse
# 子应用视图
from django.views import View
from rest_framework import status, filters, viewsets
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from rest_framework.generics import GenericAPIView, ListCreateAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.renderers import JSONRenderer
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import mixins
from projects.models import Projects
# from projects.serializers import ProjectSerializer
from projects.serializers import PorjectModelSerializer
from utils.pagination import PageNumberPagination
# class ProjectsView(View):
# class ProjectsView(APIView):
# class ProjectsView(GenericAPIView):
# class ProjectsView(mixins.ListModelMixin,
# mixins.CreateModelMixin,
# GenericAPIView):
# class ProjectsViewSet(ListCreateAPIView,
# RetrieveUpdateDestroyAPIView):
# class ProjectsViewSet(viewsets.ViewSet):
# class ProjectsViewSet(mixins.ListModelMixin,
# mixins.CreateModelMixin,
# mixins.RetrieveModelMixin,
# mixins.UpdateModelMixin,
# mixins.DestroyModelMixin,
# viewsets.ViewSet):
# class ProjectsViewSet(mixins.ListModelMixin,
# mixins.CreateModelMixin,
# mixins.RetrieveModelMixin,
# mixins.UpdateModelMixin,
# mixins.DestroyModelMixin,
# viewsets.GenericViewSet):
class ProjectsViewSet(viewsets.ModelViewSet):
"""
将2个视图类合并为1个视图类
继承了视图集父类ViewSet:
1.在定义url条目时,支持给as_view()传递字典参数。
2.ViewSet继承了ViewSetMixin, views.APIView.
3.继承了ViewSetMixin,所以可以给as_view()传递字典参数
4.继承了views.APIView,所以具备APIView的所有授权,限流等功能。
"""
queryset = Projects.objects.all()
serializer_class = PorjectModelSerializer
# 2。 类视图中指定可用于过滤查询的字段search_fields = [xxx]
search_fields = ['^full_name', 'id']
# 配置过滤器类
filter_backends = [filters.SearchFilter, OrderingFilter]
# 排序字段
ordering_fields = ['full_name', 'leader', 'id']
# 指定自定义的分页器引擎(先导入)
pagination_class = PageNumberPagination
@action(methods=["GET"],detail=False)
def names(self, request, *args, **kwargs):
"自定义action,只获取所有项目id和项目名称,且不分页"
queryset = self.get_queryset()
project_list = []
for project_query_set in queryset:
project_obj = {
"id": project_query_set.id,
"full_name": project_query_set.full_name
}
project_list.append(project_obj)
return Response(project_list, status=status.HTTP_200_OK)
1.2 将用@action装饰器装饰过的names方法添加到路由条目中,使用字典映射
1.3 前端访问自定义的url路径
2.自定义获取外键关联从表的数据
2.1 自定义interfaces类视图 方法
主表获取从表数据:主表对象.从表名小写_set.all()
2.2 使用@action()装饰器装饰
若请求路径配/<int:pk>/获取某个详细信息,则detail=True
2.3 路由中新增路由条目指定字典映射
2.4 前端访问
2.5 @action()中的url_path参数和url_name参数
url_path='xxx': 内置自定义视图集方法中指定了url_path参数后,前端访问时就可以不用再用视图集方法名names来访问了,可以直接使用xxx代替
3.Router路由器类:可使用路由器对象为视图集类自动生成路由条目,无需手动配置url条目.
3.1 导入routers
from rest_framework import routers
3.2 实例化路由器对象
routers.SimpleRouter()或者router.DefaultRouter()
3.3 将需要自动生成url条目的视图集类注册到路由器对象中
3.3.1 使用路由器对象的register()方法进行注册.
3.3.2 register()方法参数:prefix指定路由前缀,viewset指定视图集类,
不可调用as_view()方法.
3.3.2 路由器对象默认只会生成通用action(create,list,retrieve,update,destroy)的url条目,
自定义的action(如:names,interfaces)需要用@action()装饰器装饰后才能被识别并
自动生成url条目.
3.4 加载路由条目
3.4.1 方式一:
urlpatterns = [path(' ', include(router.urls))]
3.4.2 方式二:
urlpatterns += router.urls
3.5 前端验证功能正常