标签:field 数据 reg update 第一个 生成 object 参数 color
class AuthorSerializer(serializers.ModelSerializer): class Meta: model = models.AuthorDatail fields = ‘__all__‘ class BookSerializer(serializers.ModelSerializer): # 必须写一个内部类,名字叫Meta class Meta: model = models.Book fields = ‘__all__‘ class PublishSerializers(serializers.ModelSerializer): class Meta: model = models.Publish fields = ‘__all__‘
class PublishView(APIView): def get(self, request): publish_list = Publish.objects.all() bs = PublishSerializers(publish_list, many=True) return JsonResponse(bs.data,safe=False) def post(self, request): # 添加一条数据 print(request.data) bs = PublishSerializers(data=request.data) if bs.is_valid(): bs.save() # 生成记录 return JsonResponse(bs.data,safe=False) else: return JsonResponse(bs.errors,safe=False) class PublishDetailView(APIView): def get(self, request, pk): publish_obj = Publish.objects.filter(pk=pk).first() bs = PublishSerializers(publish_obj, many=False) return JsonResponse(bs.data,safe=False) def put(self, request, pk): publish_obj = Publish.objects.filter(pk=pk).first() bs = PublishSerializers(data=request.data, instance=publish_obj) if bs.is_valid(): bs.save() # update return JsonResponse(bs.data) else: return JsonResponse(bs.errors) def delete(self, request, pk): Publish.objects.filter(pk=pk).delete() return JsonResponse("")
url(r‘^publish/$‘, views.PublishView.as_view()), url(r‘^publish/(?P<pk>\d+)‘, views.PublishDetailView.as_view()),
class List: def list(self,request): publish_list = Publish.objects.all() bs = PublishSerializers(publish_list, many=True) return JsonResponse(bs.data,safe=False) class Create: def create(self,request): bs = PublishSerializers(data=request.data) if bs.is_valid(): bs.save() return JsonResponse(bs.data,safe=False) else: return JsonResponse(bs.errors,safe=False) class PublishView(APIView,List,Create): def get(self,request): return self.list(request) def create(self,request): return self.create(request)
同上
class List: def list(self,request): querryset = self.querryset bs = self.serializers(querryset,many=True) return JsonResponse(bs.data, safe=False) class Create: def create(self,request): print(request.data) bs =self.serializers(data=request.data) if bs.is_valid(): bs.save() # 生成记录 return JsonResponse(bs.data,safe=False) else: return JsonResponse(bs.errors,safe=False) class PublishView(APIView,List,Create): querryset = Publish.objects.all() serializers = PublishSerializers def get(self,request): return self.list(request) def post(self,request): return self.create(request) class BookView(APIView,List,Create): querryset = Book.objects.all() serializers = BookSerializer def get(self,request): return self.list(request) def post(self,request): return self.create(request)
同上
#drf提供的封装的类
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin from rest_framework.generics import GenericAPIView class PublishView(GenericAPIView,ListModelMixin, CreateModelMixin): queryset = Publish.objects.all() serializer_class = PublishSerializers def list(self,request): return self.list(request) def create(self,request): return self.create(request) class AuthorDetail(GenericAPIView,ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin,DestroyModelMixin): queryset = AuthorDatail.objects.all() serializer_class = AuthorSerializer def get(self,request,pk): return self.retrieve(request,pk) def post(self,request,pk): return self.create(request,pk) def put(self,request,pk): return self.update(request,pk) def delete(self,request,pk): return self.delete(request,pk)
url(r‘^publish/‘, views.PublishView.as_view()), url(r‘^publish/(?P<pk>\d+)‘, views.PublishView.as_view()), url(r‘^authorDetail/‘, views.AuthorDetailView.as_view()), url(r‘^authorDetail/(?P<pk>\d+)‘,views.AuthorDetailView.as_view()),
from rest_framework.generics import ListCreateAPIView, ListAPIView, RetrieveUpdateDestroyAPIView class PublishView(ListCreateAPIView): queryset = Publish.objects.all() serializer_class = PublishSerializers class AuthorDetailView(RetrieveUpdateDestroyAPIView): queryset = AuthorDatail.objects.all() serializer_class = AuthorSerializer
同上
from rest_framework.viewsets import ModelViewSet class PublishView(ModelViewSet): queryset = Publish.objects.all() serializer_class = PublishSerializers
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 django.conf.urls import url,include from django.contrib import admin from app01 import views from rest_framework import routers # 生成一个router对象 router=routers.DefaultRouter() # 需要传两个参数,第一个参数就是匹配的路径,第二个参数,是视图类 router.register(‘publish‘,views.PublishView) urlpatterns = [ url(‘‘, include(router.urls)), ]
标签:field 数据 reg update 第一个 生成 object 参数 color
原文地址:https://www.cnblogs.com/zhaijihai/p/10133690.html