drf 也可以使用 cbv 這種視圖,不過其對 Django 的 view 做了一些封裝。drf 最底層的 view 是 APIView,它繼承 Django 的 View。所以它的 as_view() 方法與 Django 原生的類似,在它的源碼中調(diào)用了 super 方法,即 Django view 中的 as_view() 方法,而 view 的 as_view() 在其中寫了一個閉包函數(shù)返回 view,view 返回 dispatch()。注意這個 dispatch() 是 slef 的,而 self 是當(dāng)前類的,當(dāng)前類并沒有具體實現(xiàn),所以可以找到它是在 APIView 中實現(xiàn)的。來看一下 drf 中的 dispatch:
def dispatch(self, request, *args, **kwargs):
...
request = self.initialize_request(request, *args, **kwargs)
...
# 中間有一塊與 Django 的一模一樣
return self.response
中間有一個 initialize_request 引起了我們的注意。這個函數(shù)干嘛呢?看它的參數(shù)是 request,難不成是對 request 做了一層封裝:
def initialize_request(self, request, *args, **kwargs):
parser_context = self.get_parser_context(request)
return Request(
request,
parsers=self.get_parsers(),
authenticators=self.get_authenticators(),
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)
原來其確實對 Django 的 request 做了一層封裝。我們來看一看 Request 的初始化:
def __init__(self, request, parsers=None, authenticators=None,
negotiator=None, parser_context=None):
...
self._request = request
self.authenticators = authenticators or ()
...
可以看到,在 Request 類中將 Django 原始的 request 封裝為 _request。所以如果想要調(diào)用原生的 request,可以使用 request._request 即可。
回到上一步 initialize_request 中,重點關(guān)注 self.get_authenticators(),跟進(jìn)去:
def get_authenticators(self):
return [auth() for auth in self.authentication_classes]
返回了一個列表,其中存放類對象。其中 authentication_classes 是默認(rèn)配置。所以在我們的視圖類中可以寫 authentication_classes=[] ,表示認(rèn)證方法。
我們回到 drf 的 dispatch 方法中,發(fā)現(xiàn)了另一個函數(shù)
self.initial(request, *args, **kwargs)
注意這個 request 已經(jīng)是封裝過的 request 了。追蹤進(jìn)去發(fā)現(xiàn)了這樣幾行代碼
self.perform_authentication(request)
self.check_permissions(request)
self.check_throttles(request)
分別表示 認(rèn)證,權(quán)限以及限流,之后會寫一片關(guān)于 drf 組件的文章里面會提。
先來看一下 perform_authentication,它直接就是 request.user
,我們繞道之前說的 Request 封裝函數(shù)中查看 user 屬性
@property
def user(self):
if not hasattr(self, '_user'):
with wrap_attributeerrors():
self._authenticate()
return self._user
進(jìn)入 _authenticate():
def _authenticate(self):
# 嘗試依次使用每個驗證實例驗證請求。
for authenticator in self.authenticators:
try:
user_auth_tuple = authenticator.authenticate(self)
except exceptions.APIException:
self._not_authenticated()
raise
if user_auth_tuple is not None:
self._authenticator = authenticator
self.user, self.auth = user_auth_tuple
return
self._not_authenticated()
drf 寫的注釋也很好,依次使用每個驗證實例來驗證請求,前面寫到 get_authenticators() 返回一個列表,里面是每個驗證對象的實例,這里一次調(diào)用了實例的 authenticator 方法,因此我們實現(xiàn)自定義驗證方法時,需要實現(xiàn)這個方法,當(dāng)然還有另一個方法。
下一篇將 drf 的幾個主要組件。