zoukankan      html  css  js  c++  java
  • DRF的类视图(Mixin)

    from rest_framework.views import APIView
    from meituan.models import Merchant
    from django.http import Http404
    from .serializers import MerchantSerializer
    from rest_framework.response import Response
    from rest_framework import status
    from rest_framework import mixins
    
    class MerchantView(
        generics.GenericAPIView,
        mixins.ListModelMixin,
        mixins.RetrieveModelMixin,
        mixins.CreateModelMixin,
        mixins.UpdateModelMixin,
        mixins.DestroyModelMixin
    ):
        queryset = Merchant.objects.all()
        serializer_class = MerchantSerializer
    
        def get(self,request,pk=None):
            if pk:
                return self.retrieve(request)
            else:
                return self.list(request)
    
        def perform_create(self, serializer):
            # 如果想要更改添加的逻辑,那么应该重写perform_create方法
            serializer.save(created=self.request.user)
    
        def post(self,request):
            # 如果想要更改添加的逻辑,那么应该重写perform_create方法
            return self.create(request)
    
        def put(self,request,pk):
            return self.update(request,pk)
    
        def delete(self,request,pk):
            return self.destroy(request,pk)
    
  • 相关阅读:
    马哥博客作业第十六周
    马哥博客作业第十五周
    马哥博客作业第十四周
    马哥博客作业第十三周
    回调函数
    spring cloud
    jt业务实现
    dubbo框架介绍
    SOA思想
    linux设置防火墙规则-指定ip的访问权限
  • 原文地址:https://www.cnblogs.com/xshan/p/14220323.html
Copyright © 2011-2022 走看看