【DRF-13】rest-framework之视图三部曲

  • 一.mixin类编写视图
    • 1.视图
class AuthorModelSerializer(serializers.ModelSerializer):
    class Meta:
        model=models.Author
        fields='__all__'

from rest_framework import mixins,generics
class Authors(mixins.ListModelMixin,mixins.CreateModelMixin,generics.GenericAPIView):
    queryset = models.Author.objects.all()
    serializer_class = AuthorModelSerializer

    def get(self,request,*args,**kwargs):
        return self.list(request,*args,**kwargs)

    def post(self,request,*args,**kwargs):
        return self.create(request,*args,**kwargs)

class AuthorsDetailView(mixins.RetrieveModelMixin,mixins.UpdateModelMixin,mixins.DestroyModelMixin,generics.GenericAPIView):
    queryset = models.Author.objects.all()
    serializer_class = AuthorModelSerializer
    def get(self,request,*args,**kwargs):
        return self.retrieve(request,*args,**kwargs)

    def put(self,request,*args,**kwargs):
        return self.update(request,*args,**kwargs)

    def delete(self,request,*args,**kwargs):
        return self.delete(request,*args,**kwargs)
  • 2.路由
  re_path(r'^(?P<version>[v1|v2]+)/author/$', views.AuthorView.as_view()),
  re_path(r'^(?P<version>[v1|v2]+)/author/(?P<pk>\d+)$', views.AuthorDetailView.as_view()),
  • 3.注意点

    • 3.1:queryset ,serializer_class 这两个变量是必须得,queryset告知这个类这次处理的数据,serializer_class告知这个类数据处理用到的序列化组件
    • 3.2:五类mixins作用和对应的http方法
  • 二:使用通用的基于类的视图

    • 1.试图
# 第一种:mixin模块+generics模块(GenericAPIView类)编写视图
from rest_framework import mixins
from rest_framework import generics

class AuthorView(
        mixins.ListModelMixin,
        mixins.CreateModelMixin,
        generics.GenericAPIView
    ):
    # GenericAPIView继承了之前的APIView
    # !!!切记:queryset和serializer_class 这两个名字不能改!!!
    queryset = Author.objects.all()
    serializer_class = AuthorModelSerializers
    def get(self,request,*args,**kwargs):
        return self.list(request,*args,**kwargs)
    def post(self,request,*args,**kwargs):
        return self.create(request,*args,**kwargs)

class AuthorDetailView(
        mixins.RetrieveModelMixin,
        mixins.DestroyModelMixin,
        mixins.UpdateModelMixin,
        generics.GenericAPIView
    ):
    queryset = Author.objects.all()
    serializer_class = AuthorModelSerializers
    def get(self,request,*args,**kwargs):
        return self.retrieve(request,*args,**kwargs)
    def put(self,request,*args,**kwargs):
        return self.update(request,*args,**kwargs)
    def delete(self,request,*args,**kwargs):
        return self.destroy(request,*args,**kwargs)

# 第二种:generics模块下的ListCreateAPIView,RetrieveUpdateDestroyAPIView
class AuthorView(generics.ListCreateAPIView):
    queryset = Author.objects.all()
    serializer_class = AuthorModelSerializers

class AuthorDetailView(generics.RetrieveUpdateDestroyAPIView):
    queryset = Author.objects.all()
    serializer_class = AuthorModelSerializers
  • 2.路由
  re_path(r'^(?P<version>[v1|v2]+)/author/$', views.AuthorView.as_view()),
  re_path(r'^(?P<version>[v1|v2]+)/author/(?P<pk>\d+)$', views.AuthorDetailView.as_view()),
  • 3.注意点

    • 通过使用mixin类,我们使用更少的代码重写了这些视图,但我们还可以再进一步。REST框架提供了一组已经混合好(mixed-in)的通用视图,我们可以使用它来简化我们的views.py模块。
    • 到这里是第二部,但是我们还是需要实例化两个类来对应两个url,为什么要实例两个类,因为每个类都有get方法.
  • 三:viewsets模块下的ModelViewSet

    • 1.试图:
from rest_framework import viewsets
class AuthorModelView(viewsets.ModelViewSet):
    queryset = models.Author.objects.all()
    serializer_class = AuthorModelSerializer
  • 2.路由:
path('authors/', views.AuthorModelView.as_view({'get':'list','post':'create'}),name='authors'),
re_path(r'^authors/(?P<pk>\d+)/$', views.AuthorModelView.as_view({'get':'retrieve',
                                                                   'put':'update',
                                                                    'delete':'destroy'}),name='authors_detail'),
  • 四.总结
posted @ 2024-06-05 22:42  Tony_xiao  阅读(3)  评论(0编辑  收藏  举报