Django(55)GenericAPIView原始碼分析

Silent丿丶黑羽發表於2021-06-09

原始碼分析

GenericAPIView繼承自APIView,也就是在APIView基礎上再做了一層封裝,原始碼如下:

class GenericAPIView(views.APIView):
    queryset = None
    serializer_class = None

    lookup_field = 'pk'
    lookup_url_kwarg = None

    filter_backends = api_settings.DEFAULT_FILTER_BACKENDS

    pagination_class = api_settings.DEFAULT_PAGINATION_CLASS

    def get_queryset(self):
        assert self.queryset is not None, (
            "'%s' should either include a `queryset` attribute, "
            "or override the `get_queryset()` method."
            % self.__class__.__name__
        )

        queryset = self.queryset
        if isinstance(queryset, QuerySet):
            queryset = queryset.all()
        return queryset

    def get_object(self):
        queryset = self.filter_queryset(self.get_queryset())

        lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field

        assert lookup_url_kwarg in self.kwargs, (
            'Expected view %s to be called with a URL keyword argument '
            'named "%s". Fix your URL conf, or set the `.lookup_field` '
            'attribute on the view correctly.' %
            (self.__class__.__name__, lookup_url_kwarg)
        )

        filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
        obj = get_object_or_404(queryset, **filter_kwargs)

        self.check_object_permissions(self.request, obj)

        return obj

    def get_serializer(self, *args, **kwargs):
        serializer_class = self.get_serializer_class()
        kwargs.setdefault('context', self.get_serializer_context())
        return serializer_class(*args, **kwargs)

    def get_serializer_class(self):
        assert self.serializer_class is not None, (
            "'%s' should either include a `serializer_class` attribute, "
            "or override the `get_serializer_class()` method."
            % self.__class__.__name__
        )

        return self.serializer_class

    def get_serializer_context(self):
        return {
            'request': self.request,
            'format': self.format_kwarg,
            'view': self
        }

    def filter_queryset(self, queryset):
        for backend in list(self.filter_backends):
            queryset = backend().filter_queryset(self.request, queryset, self)
        return queryset

    @property
    def paginator(self):
        if not hasattr(self, '_paginator'):
            if self.pagination_class is None:
                self._paginator = None
            else:
                self._paginator = self.pagination_class()
        return self._paginator

    def paginate_queryset(self, queryset):
        if self.paginator is None:
            return None
        return self.paginator.paginate_queryset(queryset, self.request, view=self)

    def get_paginated_response(self, data):
        assert self.paginator is not None
        return self.paginator.get_paginated_response(data)

我們可以看到GenericAPIView中定義了6個類屬性和8個方法,接下來一個個分析
 

類屬性

  • queryset = None
  • serializer_class = None
  • lookup_field = 'pk'
  • lookup_url_kwarg = None
  • filter_backends = api_settings.DEFAULT_FILTER_BACKENDS
  • pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
     

queryset

  queryset是用來控制檢視返回給前端的資料。如果沒什麼邏輯,可以直接寫在檢視的類屬性中,如果邏輯比較複雜,也可以重寫get_queryset方法用來返回一個queryset物件。如果重寫了get_queryset,那麼以後獲取queryset的時候就需要通過呼叫get_queryset方法。因為queryset` 這個屬性只會呼叫一次,以後所有的請求都是使用他的快取。
 

serializer_class

  serializer_class用來驗證和序列化資料的。也是可以通過直接設定這個屬性,也可以通過重寫get_serializer_class來實現。
 

lookup_field

在檢索的時候,根據什麼引數進行檢索。預設是pk,也就是主鍵。
 

lookup_url_kwarg

在檢索的url中的引數名稱。預設沒有設定,跟lookup_field保持一致。
 

filter_backends

用於過濾查詢集的過濾器後端類的列表。預設值與DEFAULT_FILTER_BACKENDS 設定的值相同。
 

pagination_class

當分頁列出結果時應使用的分頁類。預設值與 DEFAULT_PAGINATION_CLASS 設定的值相同,即 'rest_framework.pagination.PageNumberPagination'
 

方法

  • get_queryset
  • get_object
  • get_serializer
  • get_serializer_class
  • get_serializer_context
  • filter_queryset
     

get_queryset

def get_queryset(self):
    # 斷言queryset是否不為None
    assert self.queryset is not None, (
        "'%s' should either include a `queryset` attribute, "
        "or override the `get_queryset()` method."
        % self.__class__.__name__
    )
    
    # 定義queryset屬性,獲取父類的queryset,如果父類沒有定義類屬性`queryset`,那麼預設值就是None,就會報上面斷言的錯誤 
    queryset = self.queryset
    # 如果queryset是QuerySet物件,那麼返回全部內容
    if isinstance(queryset, QuerySet):
        queryset = queryset.all()
    # 如果不是queryset,那麼直接返回
    return queryset

get_queryset預設是返回資料庫全部資料,如果想返回其他資料,需要自定義
 

get_object

def get_object(self):
    queryset = self.filter_queryset(self.get_queryset())
    
    # 查詢過濾的條件,預設是pk
    lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field

    assert lookup_url_kwarg in self.kwargs, (
        'Expected view %s to be called with a URL keyword argument '
        'named "%s". Fix your URL conf, or set the `.lookup_field` '
        'attribute on the view correctly.' %
        (self.__class__.__name__, lookup_url_kwarg)
    )
    
    filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
    obj = get_object_or_404(queryset, **filter_kwargs)
    
    # 可能會引發許可權被拒絕
    self.check_object_permissions(self.request, obj)

    return obj

該方法是用於在資料檢索(通過pk查詢)的時候,返回一條資料的。
 

get_serializer

def get_serializer(self, *args, **kwargs):
    serializer_class = self.get_serializer_class()
    kwargs.setdefault('context', self.get_serializer_context())
    return serializer_class(*args, **kwargs)

返回應該用於驗證和反序列化輸入以及序列化輸出的序列化器例項
 

get_serializer_class

def get_serializer_class(self):
    assert self.serializer_class is not None, (
        "'%s' should either include a `serializer_class` attribute, "
        "or override the `get_serializer_class()` method."
        % self.__class__.__name__
    )

    return self.serializer_class

返回用於序列化的類。預設使用self.serializer_class。如果您需要根據傳入請求提供不同的序列化,您可能需要重寫它。
 

get_serializer_context

def get_serializer_context(self):
    return {
        'request': self.request,
        'format': self.format_kwarg,
        'view': self
    }

提供給序列化的額外上下文。
 

filter_queryset

def filter_queryset(self, queryset):
    for backend in list(self.filter_backends):
        queryset = backend().filter_queryset(self.request, queryset, self)
    return queryset

給定一個查詢集,使用正在使用的過濾器對其進行過濾。您不太可能想要覆蓋此方法,但如果您想將配置的過濾後端應用到預設查詢集,您可能需要從列表檢視或自定義get_object 方法中呼叫它。
 

實戰案例

class StudentsGenericView(GenericAPIView):
    queryset = Student.objects.all()  # 定義了類屬性queryset,告訴檢視要針對哪個模型做處理
    serializer_class = StudentModelSerializer  # 定義類屬性serlializer_class,告訴檢視你的序列化的類是什麼
    def get(self, request, *args, **kwargs):
        pk = kwargs.get("pk")
        if pk:
            many = False
            query = self.get_object()  # 通過pk檢索資料,返回一條資料
        else:
            many = True
            query = self.get_queryset()  # 返回動態的資料集,預設返回全部
        serializer = self.get_serializer(query, many=many)  # 如果資料物件是queryset物件,many需要為True
        return APIResponse(results=serializer.data)

相關文章