day86

视图组件

  基于以往我们所用的视图函数,我们发现其中冗余的代码比较多,今天就来对其进行封装,争取做一个代码洁癖者

  原来我们的视图函数:

class Book(APIView):
    def get(self, request):
        book = models.Books.objects.all()
        book_ser = Ser.Books(book, many=True)
        return JsonResponse(book_ser.data, safe=False)

    def post(self, request):
        new_book = Ser.Books(data=request.data)
        if new_book.is_valid():
            new_book.save()
            return JsonResponse(new_book.data)
        else:
            return JsonResponse(new_book.errors)


class BookDetail(APIView):
    def get(self, request, pk):
        book = models.Books.objects.filter(pk=pk).first()
        book_ser = Ser.Books(book, many=False)
        return JsonResponse(book_ser.data, safe=False)

    def put(self, request,pk):
        book = models.Books.objects.filter(pk=pk).first()
        new_book = Ser.Books(data=request.data,instance=book)
        if new_book.is_valid():
            new_book.save()
            return JsonResponse(new_book.data)
        else:
            return JsonResponse(new_book.errors)
View Code

手动一层封装:

  创建了List和Create辅助类,节省了代码

class List:
    def list(self, request):
        queryset = self.query
        info = self.serializers(queryset, many=True)
        return JsonResponse(info.data, safe=False)


class Create:
    def create(self, request):
        new = self.serializers(data=request.data)
        if new.is_valid():
            new.save()
            return JsonResponse(new.data, safe=False)
        else:
            return JsonResponse(new.errors)


class Book(APIView, List, Create):
    query = models.Books.objects.all()
    serializers = Ser.Books

    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)


class Publish(APIView,List,Create):
    query = models.Publish.objects.all()
    serializers = Ser.Publish

    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)
View Code

使用drf自带的二层封装:

  说明:

'''
CreateModelMixin: 增加
DestroyModelMixin:删
UpdateModelMixin:改
ListModelMixin:查全部
RetrieveModelMixin:查一条
'''
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin, UpdateModelMixin, \
    RetrieveModelMixin


class Book(GenericAPIView, ListModelMixin, CreateModelMixin):
    queryset = models.Books.objects.all()
    serializer_class = Ser.Books

    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)


class Publish(GenericAPIView, ListModelMixin, CreateModelMixin):
    queryset = models.Publish.objects.all()
    serializer_class = Ser.Publish

    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)


class BookDetail(GenericAPIView, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin):
    queryset = models.Books.objects.all()
    serializer_class = Ser.Books

    def get(self, request, pk):
        return self.retrieve(request, pk)

    def put(self, request, pk):
        return self.update(request, pk)

    def delete(self, request, pk):
        return self.destroy(request, pk)
View Code

第三层封装

  drf中自己写了一个类继承了封装的类里面写了get、post等方法

from rest_framework.generics import RetrieveDestroyAPIView, ListCreateAPIView


# class Book(GenericAPIView, ListModelMixin, CreateModelMixin):
class Book(ListCreateAPIView):
    queryset = models.Books.objects.all()
    serializer_class = Ser.Books


# class BookDetail(GenericAPIView, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin):
class BookDetail(RetrieveDestroyAPIView):
    queryset = models.Books.objects.all()
    serializer_class = Ser.Books
View Code

最终的封装(不建议使用)

说明:

'''
终极封装是因为这个类继承了CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin,ListModelMixin,GenericViewSet):
注:在路由中配置时需要传递key:value对应的方法参数,保证对应的方法有对应的参数
{'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}
'''
from rest_framework.viewsets import ModelViewSet
from rest_framework.renderers import JSONOpenAPIRenderer, BrowsableAPIRenderer, HTMLFormRenderer


class Book(ModelViewSet):
    # renderer_classes = [JSONOpenAPIRenderer, BrowsableAPIRenderer, HTMLFormRenderer]
    queryset = models.Books.objects.all()
    serializer_class = Ser.Books


class Publish(ModelViewSet):
    queryset = models.Publish.objects.all()
    serializer_class = Ser.Publish
View Code

魔法方法(重点)

# 建议用魔法类ViewSetMixin
from rest_framework.viewsets import  ViewSetMixin

 

转载于:https://www.cnblogs.com/yaoxiaofeng/p/10134058.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值