标签: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)
标签:ram post serial 框架 object 视图 lis gen generic
原文地址:http://blog.51cto.com/dzm911/2336643