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

rest_framake之视图

时间:2018-12-17 21:08:28      阅读:172      评论:0      收藏:0      [点我收藏+]

标签: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__

views

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()),

二  小试牛刀

views

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)

url

同上

三  这次是初生牛犊之牛刀

views

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)

url

同上

四  牛刀

views


#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

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()),

五  牛!牛!刀!

views

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

url

同上

六  牛!牛!牛!刀

views

from rest_framework.viewsets import ModelViewSet


class PublishView(ModelViewSet):
    queryset = Publish.objects.all()
    serializer_class = PublishSerializers

url

    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})),

七  究极版之牛都秃顶了

views

同上

url

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)),
]

 

rest_framake之视图

标签:field   数据   reg   update   第一个   生成   object   参数   color   

原文地址:https://www.cnblogs.com/zhaijihai/p/10133690.html

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