在URL控制器中
from django.conf.urls import url
from app02 import views
app_name = "app02"
urlpatterns = [
url(r'^book/$', views.BookView.as_view(), name="book"),
url(r'^book/(?P<pk>\d+)/$', views.BookDetailView.as_view(), name="book_detail"),
url(r'^publish/$', views.PublishView.as_view(), name="publish"),
url(r'^publish/(?P<pk>\d+)/$', views.PublishDetailView.as_view(), name="publish_detail"),
url(r'^author/$', views.AuthorView.as_view(), name="author"),
url(r'^author/(?P<pk>\d+)/$', views.AuthorDetailView.as_view(), name="author_detail"),
]
django.core.views.View
方式一:Django的序列化组件
在views.py中
from django.views import View
from django.core.serializers import serialize
class BookView(View):
def get(self, request):
queryset = Book.objects.all()
queryset = serialize("json", queryset)
return HttpResponse(queryset)
rest_framework.views.APIView
方式二:restframework序列化
在views.py中
class BookView(APIView):
def get(self, request):
book_qs = Book.objects.all()
bs = dataApi.BookSerializers(book_qs, many=True)
return Response(bs.data)
def post(self, request):
book_qs = dataApi.BookSerializers(data=request.data, many=True)
if book_qs.is_valid():
book_qs.save()
return HttpResponse(book_qs.data)
else:
return HttpResponse(book_qs.errors)
class BookDetailView(APIView):
def get(self, request, pk):
book_qs = Book.objects.filter(pk=pk).first()
ps = dataApi.BookSerializers(book_qs, many=False)
return Response(ps.data)
def put(self, request, pk):
book_qs = Book.objects.filter(pk=pk).first()
bs = dataApi.BookSerializers(data=request.data, instance=book_qs)
if bs.is_valid():
bs.save()
return HttpResponse(bs.data)
else:
return HttpResponse(bs.errors)
def delete(self, requset, pk):
Book.objects.filter(pk=pk).delete()
return HttpResponse("%s 已删除" % requset.data)
class PublishView(APIView):
def get(self, requset):
publish_qs = Publish.objects.all()
ps = dataApi.PublishSerializers(publish_qs, many=True)
return Response(ps.data)
def post(self, request):
publish_qs = dataApi.PublishSerializers(data=request.data, many=True)
if publish_qs.is_valid():
publish_qs.save()
return HttpResponse(publish_qs.data)
else:
return HttpResponse(publish_qs.errors)
class PublishDetailView(APIView):
def get(self, request, pk):
publish_qs = Publish.objects.filter(pk=pk).first()
ps = dataApi.PublishSerializers(publish_qs, many=False)
return Response(ps.data)
def put(self, request, pk):
p_obj = Publish.objects.filter(pk=pk).first()
ps = dataApi.PublishSerializers(data=request.data, instance=p_obj)
if ps.is_valid():
ps.save()
return HttpResponse(ps.data)
else:
return HttpResponse(ps.errors)
def delete(self, request, pk):
Publish.objects.filter(pk=pk).delete()
return HttpResponse("删除成功")
class AuthorView(APIView):
def get(self, request):
book_qs = Author.objects.all()
As = dataApi.AuthorSerializers(book_qs, many=True)
return Response(As.data)
def post(self, request):
As = dataApi.AuthorSerializers(request.data, many=True)
if As.is_valid():
As.save()
return HttpResponse(As.data)
else:
return HttpResponse(As.errors)
class AuthorDetailView(APIView):
def get(self, request, pk):
As_qs = Author.objects.filter(pk=pk).first()
As = dataApi.AuthorSerializers(As_qs, many=False)
return Response(As.data)
def put(self, request, pk):
As_qs = Author.objects.filter(pk=pk).first()
As = dataApi.AuthorSerializers(data=request.data, instance=As_qs)
if As.is_valid():
As.save()
return HttpResponse(As.data)
else:
return HttpResponse(As.errors)
def delete(self, request, pk):
Author.objects.filter(pk=pk).delete()
return HttpResponse(request.data)
方式三:用继承简化代码
在views.py中
class BaseView(object):
def __init__(self, qss, serialize_class):
self.qss = qss
self.serialize_class = serialize_class
def get(self, request):
qs = self.serialize_class(self.qss, many=True)
return Response(qs.data)
def post(self, request):
qs = self.serialize_class(data=request.data, many=True)
if qs.is_valid():
qs.save()
return HttpResponse(qs.data)
else:
return HttpResponse(qs.errors)
class BaseDetailView(object):
def __init__(self, qss, serialize_class):
self.qss = qss
self.serialize_class = serialize_class
def get(self, request, pk):
obj = self.qss.filter(pk=pk).first()
qs = self.serialize_class(obj, many=False)
return Response(qs.data)
def put(self, request, pk):
obj = self.qss.filter(pk=pk).first()
qs = self.serialize_class(data=request.data, instance=obj)
if qs.is_valid():
qs.save()
return HttpResponse(qs.data)
else:
return HttpResponse(qs.errors)
def delete(self, request, pk):
self.qss.filter(pk=pk).delete()
return HttpResponse(request.data)
class BookView(APIView, BaseView):
qss = Book.objects.all()
serialize_class = dataApi.BookSerializers
class BookDetailView(APIView, BaseDetailView):
qss = Book.objects.all()
serialize_class = dataApi.BookSerializers
class PublishView(APIView, BaseView):
qss = Publish.objects.all()
serialize_class = dataApi.PublishSerializers
class PublishDetailView(APIView, BaseDetailView):
qss = Publish.objects.all()
serialize_class = dataApi.PublishSerializers
class AuthorView(APIView, BaseView):
qss = Author.objects.all()
serialize_class = dataApi.AuthorSerializers
class AuthorDetailView(APIView, BaseDetailView):
qss = Author.objects.all()
serialize_class = dataApi.AuthorSerializers
rest_framework序列化接口
方式一:mixin类编写视图
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import CreateModelMixin, ListModelMixin, RetrieveModelMixin, UpdateModelMixin, \
DestroyModelMixin
class BookView(CreateModelMixin, ListModelMixin, GenericAPIView):
queryset = Book.objects.all()
serializer_class = dataApi.BookSerializers
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 BookDetailView(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):
queryset = Book.objects.all()
serializer_class = dataApi.BookSerializers
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)
class PublishView(CreateModelMixin, ListModelMixin, GenericAPIView):
queryset = Publish.objects.all()
serializer_class = dataApi.PublishSerializers
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 PublishDetailView(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):
queryset = Publish.objects.all()
serializer_class = dataApi.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)
class AuthorView(CreateModelMixin, ListModelMixin, GenericAPIView):
queryset = Author.objects.all()
serializer_class = dataApi.AuthorSerializers
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(RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin, GenericAPIView):
queryset = Author.objects.all()
serializer_class = dataApi.AuthorSerializers
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)
方式二:使用通用的基于类的视图
from rest_framework.generics import ListCreateAPIView,RetrieveUpdateDestroyAPIView
class BookView(ListCreateAPIView):
queryset = Book.objects.all()
serializer_class = dataApi.BookSerializers
class BookDetailView(RetrieveUpdateDestroyAPIView):
queryset = Book.objects.all()
serializer_class = dataApi.BookSerializers
class PublishView(ListCreateAPIView):
queryset = Publish.objects.all()
serializer_class = dataApi.PublishSerializers
class PublishDetailView(RetrieveUpdateDestroyAPIView):
queryset = Publish.objects.all()
serializer_class = dataApi.PublishSerializers
class AuthorView(ListCreateAPIView):
queryset = Author.objects.all()
serializer_class = dataApi.AuthorSerializers
class AuthorDetailView(RetrieveUpdateDestroyAPIView):
queryset = Author.objects.all()
serializer_class = dataApi.AuthorSerializers
方式三:viewsets.ModelViewSet
在urls.py中
from django.conf.urls import url
from app02 import views
app_name = "app02"
urlpatterns = [
url(r'^book/$', views.BookView.as_view({"get": "list", "post": "create"}), name="book"),
url(r'^book/(?P<pk>\d+)/$', views.BookView.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy'
}),name="book_detail"),
url(r'^publish/$', views.PublishView.as_view({
"get": "list", "post": "create"
}), name="publish"),
url(r'^publish/(?P<pk>\d+)/$', views.PublishView.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy'
}),name="publish_detail"),
url(r'^author/$', views.AuthorView.as_view({
"get": "list", "post": "create"
}), name="author"),
url(r'^author/(?P<pk>\d+)/$', views.AuthorView.as_view({
'get': 'retrieve',
'put': 'update',
'patch': 'partial_update',
'delete': 'destroy'
}),name="author_detail")]
在views.py文件中
from rest_framework.viewsets import ModelViewSet
class BookView(ModelViewSet):
queryset = Book.objects.all()
serializer_class = dataApi.BookSerializers
class PublishView(ModelViewSet):
queryset = Publish.objects.all()
serializer_class = dataApi.PublishSerializers
class AuthorView(ModelViewSet):
queryset = Author.objects.all()
serializer_class = dataApi.AuthorSerial