rest_framake之视图

Posted zhaijihai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rest_framake之视图相关的知识,希望对你有一定的参考价值。

一  最原始的写法

前戏之序列化

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之视图的主要内容,如果未能解决你的问题,请参考以下文章

回收站视图未显示在片段中

如何从片段内的列表视图打开链接网址?

如何使列表视图出现在片段中?

转到不在底部导航视图内的片段并返回后,底部导航视图不起作用

如何从活动中更改片段中视图的可见性

片段中的网格视图