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

DRF--十大接口方法:单查群查,单删群删,单增群增,整体单改群改,局部单改群改

时间:2019-12-27 22:05:14      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:建议   就是   first   dict   出错   list   work   pop   就会   

from rest_framework.views import APIView
from rest_framework.response import Response
from . import models, serializers
from .response import APIResponse


# 出版社群查
class PublishAPIView(APIView):
    def get(self, request, *args, **kwargs):
        publish_query = models.Publish.objects.all()
        publish_ser = serializers.PublishModelSerializer(publish_query, many=True)
        return APIResponse(results=publish_ser.data)


class BookAPIView(APIView):
    # 单查群查
    def get(self, request, *args, **kwargs):
        pk = kwargs.get('pk')
        if pk:
            book_obj = models.Book.objects.filter(is_delete=False, pk=pk).first()
            book_ser = serializers.BookModelSerializer(book_obj)
        else:
            book_query = models.Book.objects.filter(is_delete=False).all()
            book_ser = serializers.BookModelSerializer(book_query, many=True)
        return APIResponse(results=book_ser.data)
        # return Response(data=book_ser.data)

    # 单删群删
    def delete(self, request, *args, **kwargs):
        """
        单删:接口:/books/(pk)/   数据:空
        群删:接口:/books/   数据:[pk1, ..., pkn]
        逻辑:修改is_delete字段,修改成功代表删除成功,修改失败代表删除失败
        """
        pk = kwargs.get('pk')
        if pk:
            pks = [pk]  # 将单删格式化成群删一条
        else:
            pks = request.data  # 群删
        try:  # 数据如果有误,数据库执行会出错
            rows = models.Book.objects.filter(is_delete=False, pk__in=pks).update(is_delete=True)
        except:
            return APIResponse(1, '数据有误')

        if rows:
            return APIResponse(0, '删除成功')
        return APIResponse(1, '删除失败')

    # 单增群增
    def post(self, request, *args, **kwargs):
        """
        单增:接口:/books/   数据:{...}
        群增:接口:/books/   数据:[{...}, ..., {...}]
        逻辑:将数据给系列化类处理,数据的类型关系到 many 属性是否为True
        """
        if isinstance(request.data, dict):
            many = False
        elif isinstance(request.data, list):
            many = True
        else:
            return Response(data={'detail': '数据有误'}, status=400)

        book_ser = serializers.BookModelSerializer(data=request.data, many=many)
        book_ser.is_valid(raise_exception=True)
        book_obj_or_list = book_ser.save()
        return APIResponse(results=serializers.BookModelSerializer(book_obj_or_list, many=many).data)


    # 整体单改群改
    def put(self, request, *args, **kwargs):
        """
        单改:接口:/books/(pk)/   数据:{...}
        群增:接口:/books/   数据:[{pk, ...}, ..., {pk, ...}]
        逻辑:将数据给系列化类处理,数据的类型关系到 many 属性是否为True
        """
        pk = kwargs.get('pk')
        if pk:  # 单改
            try:
                # 与增的区别在于,需要明确被修改的对象,交给序列化类
                book_instance = models.Book.objects.get(is_delete=False, pk=pk)
            except:
                return Response({'detail': 'pk error'}, status=400)

            book_ser = serializers.BookModelSerializer(instance=book_instance, data=request.data)
            book_ser.is_valid(raise_exception=True)
            book_obj = book_ser.save()
            return APIResponse(results=serializers.BookModelSerializer(book_obj).data)
        else:  # 群改
            # 分析(重点):
            # 1)数据是列表套字典,每个字典必须带pk,就是指定要修改的对象,如果有一条没带pk,整个数据有误
            # 2)如果pk对应的对象已被删除,或是对应的对象不存在,可以认为整个数据有误(建议),可以认为将这些错误数据抛出即可
            request_data = request.data
            try:
                pks = []
                for dic in request_data:
                    pk = dic.pop('pk')  # 解决分析1,没有pk pop方法就会抛异常
                    pks.append(pk)

                book_query = models.Book.objects.filter(is_delete=False, pk__in=pks).all()
                if len(pks) != len(book_query):
                    raise Exception('pk对应的数据不存在')
            except Exception as e:
                return Response({'detail': '%s' % e}, status=400)

            book_ser = serializers.BookModelSerializer(instance=book_query, data=request_data, many=True)
            book_ser.is_valid(raise_exception=True)
            book_list = book_ser.save()
            return APIResponse(results=serializers.BookModelSerializer(book_list, many=True).data)



    # 局部单改群改
    def patch(self, request, *args, **kwargs):
        pk = kwargs.get('pk')
        if pk:  # 单改
            try:
                book_instance = models.Book.objects.get(is_delete=False, pk=pk)
            except:
                return Response({'detail': 'pk error'}, status=400)
            # 设置partial=True的序列化类,参与反序列化的字段,都会置为选填字段
            # 1)提供了值得字段发生修改。
            # 2)没有提供的字段采用被修改对象原来的值

            # 设置context的值,目的:在序列化完成自定义校验(局部与全局钩子)时,可能需要视图类中的变量,如请求对象request
            # 可以通过context将其传入,在序列化校验方法中,self.context就能拿到传入的视图类中的变量
            book_ser = serializers.BookModelSerializer(instance=book_instance, data=request.data, partial=True, context={'request': request})
            book_ser.is_valid(raise_exception=True)
            book_obj = book_ser.save()
            return APIResponse(results=serializers.BookModelSerializer(book_obj).data)
        else:  # 群改
            request_data = request.data
            try:        
                pks = []
                for dic in request_data:
                    pk = dic.pop('pk')
                    pks.append(pk)

                book_query = models.Book.objects.filter(is_delete=False, pk__in=pks).all()
                if len(pks) != len(book_query):
                    raise Exception('pk对应的数据不存在')
            except Exception as e:
                return Response({'detail': '%s' % e}, status=400)

            book_ser = serializers.BookModelSerializer(instance=book_query, data=request_data, many=True, partial=True)
            book_ser.is_valid(raise_exception=True)
            book_list = book_ser.save()
            return APIResponse(results=serializers.BookModelSerializer(book_list, many=True).data)







DRF--十大接口方法:单查群查,单删群删,单增群增,整体单改群改,局部单改群改

标签:建议   就是   first   dict   出错   list   work   pop   就会   

原文地址:https://www.cnblogs.com/ludundun/p/12109641.html

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