zoukankan      html  css  js  c++  java
  • DRF-类视图(GenericAPIView)

    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 generics
    
    class MerchantView(
        generics.CreateAPIView,
        generics.UpdateAPIView,
        generics.DestroyAPIView,
        generics.RetrieveAPIView
    ):
        queryset = Merchant.objects.all()
        serializer_class = MerchantSerializer
        # lookup_field = 'name'
    
    class MerthantListView(
        generics.ListAPIView
    ):
        queryset = Merchant.objects.all()
        serializer_class = MerchantSerializer
    
    • get_queryset(self):
      • 通过重写该方法实现复杂查询后返回queryset对象。return queryset
    • get_object(self):
      • 通过复写该方法实现数据检索时,返回一条数据
    • perform_creat(self,serializer):
      • 保存时调用的方法
    • perform_update(self,serializer):
      • 更新对象时调用
    • perform_destroy(self,serializer):
      • 删除对象的时候调用
  • 相关阅读:
    分答是什么?
    判定表
    总结
    周结
    第五周周结
    周结
    一周总结(18周)
    一周总结(17周)
    一周总结(16周)
    一周总结(15周)
  • 原文地址:https://www.cnblogs.com/xshan/p/14220326.html
Copyright © 2011-2022 走看看