码迷,mamicode.com
首页 > 其他好文 > 详细

Django REST framework基础:视图和路由

时间:2019-11-11 00:19:41      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:style   create   objects   port   mil   路由   load   ted   gen   

在原来modelserializers基础上进行了一次封装方便了使用

#
author class AuthorModelSerializers(serializers.ModelSerializer): class Meta: model = models.Author fields = "__all__" from rest_framework import mixins from rest_framework import generics class AuthorView(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView): # queryset serializer 这两个方法一定要定义成这个不然取不到值 queryset = models.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 AuthorDetaiView(mixins.RetrieveModelMixin, mixins.DestroyModelMixin, mixins.UpdateModelMixin, generics.GenericAPIView): # queryset serializer 这两个方法一定要定义成这个不然取不到值 queryset = models.Author.objects.all() serializer_class = AuthorModelSerializers def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) def delete(self,request,*args,**kwargs): return self.destroy(request,*args,**kwargs) def put(self,request,*args,**kwargs): return self.update(request,*args,**kwargs)

使用通用的基于类的视图(封装第三次减少了代码量)

# author
class AuthorModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = models.Author
        fields = "__all__"


from rest_framework import mixins
from rest_framework import generics


# 封装了3层
class AuthorView(generics.ListCreateAPIView):
    # queryset serializer 这两个方法一定要定义成这个不然取不到值
    queryset = models.Author.objects.all()
    serializer_class = AuthorModelSerializers


# 封装了3层
class AuthorDetaiView(generics.RetrieveUpdateDestroyAPIView):
    # queryset serializer 这两个方法一定要定义成这个不然取不到值
    queryset = models.Author.objects.all()
    serializer_class = AuthorModelSerializers

使用视图集
viewsets.ModelViewSet

原理:
技术图片

 


 技术图片

 




# author


class AuthorModelSerializers(serializers.ModelSerializer):
class Meta:
model = models.Author
fields = "__all__"


from rest_framework import mixins
from rest_framework import generics


class AuthorView(mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
# queryset serializer 这两个方法一定要定义成这个不然取不到值
queryset = models.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 AuthorDetaiView(mixins.RetrieveModelMixin, mixins.DestroyModelMixin, mixins.UpdateModelMixin,
generics.GenericAPIView):
# queryset serializer 这两个方法一定要定义成这个不然取不到值
queryset = models.Author.objects.all()
serializer_class = AuthorModelSerializers

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

def delete(self,request,*args,**kwargs):
return self.destroy(request,*args,**kwargs)

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

Django REST framework基础:视图和路由

标签:style   create   objects   port   mil   路由   load   ted   gen   

原文地址:https://www.cnblogs.com/Rivend/p/11832388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!