一、GenericViewSet类的使用
继承自GenericAPIView
,作用也与GenericAPIVIew类似,提供了get_object、get_queryset等方法便于视图的开发
1.1、代码
from rest_framework.viewsets import GenericViewSet
from rest_framework.response import Response
from rest_framework import statusfrom rest_app2.models import Student
from rest_app2.serializers import StudentSerializerclass StudentList(GenericViewSet):# 指定查询集queryset = Student.objects.all()# 指定序列化器,不用创建对象serializer_class = StudentSerializer# 在GenericAPIView中,方法名是get,在GenericViewSet中,方法名是listdef list(self,request):# 查询所有数据stus = self.get_queryset()# 将数据转换为json格式, st