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)
上一篇:React 代码共享最佳实践方式


下一篇:Vue: 组件扩展