一、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 status
from rest_app2.models import Student
from rest_app2.serializers import StudentSerializer
class StudentList(GenericViewSet):
# 指定查询集
queryset = Student.objects.all()
# 指定序列化器,不用创建对象
serializer_class = StudentSerializer
# 在GenericAPIView中,方法名是get,在GenericViewSet中,方法名是list
def list(self,request):
# 查询所有数据
stus = self.get_queryset()
# 将数据转换为json格式, st