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

Django REST_framework框架 02

时间:2018-12-28 22:27:29      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:ram   post   serial   框架   object   视图   lis   gen   generic   

mixin类编写视图

urls

from app01 import views
urlpatterns = [
    ......
    url(r‘^authors/$‘, views.AuthorView.as_view(), name="author"),
    url(r‘^authors/(?P<pk>\d+)/$‘, views.AuthorDetailView.as_view(), name="detail_author"),
]

还要写一个ModelSerializer,方法与上一篇博文中相同
views

from rest_framework import mixins
from rest_framework import generics

#GenericAPIView继承了APIView
class AuthorView(mixins.ListModelMixin,    #查看所有
                 mixins.CreateModelMixin,  #添加
                 generics.GenericAPIView):
    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.DestroyModelMixin,   #删除
                       mixins.RetrieveModelMixin,  #查看单条
                       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)

Django REST_framework框架 02

标签:ram   post   serial   框架   object   视图   lis   gen   generic   

原文地址:http://blog.51cto.com/dzm911/2336643

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