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

rest-framework之视图

时间:2018-12-19 01:14:50      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:code   ext   分享   man   编写   sel   top   出版社   elf   

一 基本视图

写一个出版社的增删查改resful接口

路由:

    url(r^publish/$, views.PublishView.as_view()),
    url(r^publish/(?P<pk>\d+)/$, views.PublishDetailView.as_view()),

视图:

技术分享图片
class PublishSerializers(serializers.ModelSerializer):
    class Meta:
        model=models.Publish
        fields=__all__

class PublishView(APIView):

    def get(self, request):
        publish_list = models.Publish.objects.all()
        bs = PublishSerializers(publish_list, many=True)
        # 序列化数据

        return Response(bs.data)

    def post(self, request):
        # 添加一条数据
        print(request.data)

        bs=PublishSerializers(data=request.data)
        if bs.is_valid():
            bs.save()  # 生成记录
            return Response(bs.data)
        else:

            return Response(bs.errors)

class PublishDetailView(APIView):
    def get(self,request,pk):
        publish_obj=models.Publish.objects.filter(pk=pk).first()
        bs=PublishSerializers(publish_obj,many=False)
        return Response(bs.data)
    def put(self,request,pk):
        publish_obj = models.Publish.objects.filter(pk=pk).first()

        bs=PublishSerializers(data=request.data,instance=publish_obj)
        if bs.is_valid():
            bs.save() # update
            return Response(bs.data)
        else:
            return Response(bs.errors)
    def delete(self,request,pk):
        models.Publish.objects.filter(pk=pk).delete()

        return Response("")
技术分享图片

二 mixin类和generice类编写视图

技术分享图片
from rest_framework.mixins import CreateModelMixin,RetrieveModelMixin,ListModelMixin,UpdateModelMixin,DestroyModelMixin
from rest_framework.generics import GenericAPIView
class PublishView(ListModelMixin,CreateModelMixin,GenericAPIView):
    queryset=models.Publish.objects.all()
    serializer_class=PublishSerializers

    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)

class PublishDetailView(RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,GenericAPIView):
    queryset=models.Publish.objects.all()
    serializer_class=PublishSerializers
    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

技术分享图片
from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView
class PublishView(ListCreateAPIView):
    queryset=models.Publish.objects.all()
    serializer_class=PublishSerializers

class PublishDetailView(RetrieveUpdateDestroyAPIView):
    queryset=models.Publish.objects.all()
    serializer_class=PublishSerializers
技术分享图片

四 使用ModelViewSet

路由:

    url(r^publish/$, views.PublishView.as_view({get:list,post:create})),
    url(r^publish/(?P<pk>\d+)/$, views.PublishView.as_view({get:retrieve,put:update,delete:destroy})),

视图:

from rest_framework.viewsets import ModelViewSet
class PublishView(ModelViewSet):
    queryset=models.Publish.objects.all()
    serializer_class=PublishSerializers

 

rest-framework之视图

标签:code   ext   分享   man   编写   sel   top   出版社   elf   

原文地址:https://www.cnblogs.com/di2wu/p/10140961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!