drf中 連表深度查詢和ListSerializer類包括drf中Response二次封裝

drf中Response二次封裝:

from rest_framework.response import Response
'''
return MyResponse(status=1,msg='error',results=[],token='a.b.c',http_status=400,exception=True)

Response模塊的響應數據 data,最終數據results,Response模塊須要向前臺展現的通常有響應數據data(包括data裏面的results),網絡狀態碼status,是否異常exception

return Response(
     data={
        'status':1,
        'msg':'error',
        'results':[],
        token:'a.b.c'
        },
        status=400,
        exception=True
'''
class APIResponse(Response):
    def __init__(self,status=0,msg='ok',results=None,http_status=None,
                 headers=None,exception=False,content_type=None,**kwargs):
        #將status、msg、results、kwargs格式化成data
        data={
            'status':status,
            'msg':msg,
        }
        #results只要不爲空都是數據:False、0、''都是數據==》條件不能寫if results
        if results is not None:
            data['results']=results
        #將kwargs中額外的k-v數據添加data中  字典的update方法
        data.update(**kwargs)
        super().__init__(data=data,status=http_status,headers=headers,exception=exception,content_type=content_type)

連表深度查詢:

  • 外鍵字段默認顯示的是外鍵值(int類型),不會本身進行深度查詢
  • 深度查詢方式:
    • 子序列化:必須有子序列化類配合,不能反序列化
    • 配置depth:自動深度查詢的是關聯表的全部字段,數據量太多
    • 插拔式@property:名字不能與外鍵名同名
#serializers.py
class BookModelSerializer(serializers.ModelSerializer):
  
    class Meta:
        list_serializer_class = BookListSerializer

        model = models.Book
        fields = ['name', 'price', 'publish', 'authors', 'publish_info', 'author_list']
        extra_kwargs = {
            'publish': {
                'write_only': True
            },
            'authors': {
                'write_only': True
            }
        }
        
#models.py
class Book(BaseModel):
    name = models.CharField(max_length=64)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    publish = models.ForeignKey(to='Publish', related_name='books', db_constraint=False, on_delete=models.DO_NOTHING, null=True)
    authors = models.ManyToManyField(to='Author', related_name='books', db_constraint=False)

    @property
    def publish_info(self):  #單個數據
        return {
            'name': self.publish.name,
            'address':self.publish.address,

        }

    @property
    def author_list(self):
        author_list_temp = []  # 存放全部做者格式化成數據的列表
        authors=self.authors.all()  #全部做者
        for author in authors:  #遍歷處理全部做者
            author_dic={
                'name':author.name,
            }
            try: #有詳情才處理詳情信息
                author_dic['mobile']=author.detail.mobile
            except:
                author_dic['mobile']=''
            author_list_temp.append(author_dic)   #將處理過的數據添加到數據列表中
        return author_list_temp  #返回處理後的結果


    def __str__(self):
        return self.name

單查羣查接口:

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 Response({
            'status': 0,
            'msg': 'ok',
            'results': 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)

 

ListSerializer類分析

  • ModelSerializer默認沒有羣改功能,因此它配置了ListSerializer輔助類,幫助完成羣增羣改,須要在ModelSerializer類的Meta中設置list_serializer_class
  • 自定義ListSerializer子類,將子類綁定給list_serializer_class
  • 若是除了羣增還有羣改,就不須要自定義配置,可是須要羣改,必須自定義配置
class BookListSerializer(serializers.ListSerializer):
    # 自定義的羣增羣改輔助類,沒有必要重寫create方法
    def create(self, validated_data):
        return super().create(validated_data)

    def update(self, instance_list, validated_data_list):
        return [
            self.child.update(instance_list[index], attrs) for index, attrs in enumerate(validated_data_list)
        ]

put方法的總體單改和羣改:

# 總體單改羣改
    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)

局部單改羣改:

  • 設置partial=True的序列化類,參與反序列化的字段,都會置爲選填字段
    • 提供了值得字段發生修改。
    • 沒有提供的字段採用被修改對象原來的值
  • 設置context的值,目的:在序列化完成自定義校驗(局部與全局鉤子)時,可能須要視圖類中的變量,如請求對象request
  • 能夠經過context將其傳入,在序列化校驗方法中,self.context就能拿到傳入的視圖類中的變量
# 局部單改羣改
    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)
           
            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)
相關文章
相關標籤/搜索