Django Rest Framework 原始碼相關
阿新 • • 發佈:2020-10-27
FBV和CBV區別
CBV 內部dispatch 做了分發 FBV沒辦法用類的繼承,多型 # DJango的View只做了CBV的分發 def dispatch(self, request, *args, **kwargs): # GET POST # BookView().get if request.method.lower() in self.http_method_names: handler = getattr(self, request.method.lower(), self.http_method_not_allowed)else: handler = self.http_method_not_allowed return handler(request, *args, **kwargs)
APIView 和View的區別
-- APIView繼承了View -- 重寫了as_view方法 -- 豁免csrf -- def dispatch(self, request, *args, **kwargs): self.args = args self.kwargs= kwargs # 重新封裝request # 新的request變成 Request的例項化物件 # 舊的request變成了_request request = self.initialize_request(request, *args, **kwargs) self.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 ) -- Class Request(object): self._request = 舊的request def query_params(self): return self._request.GET def data(self): return post請求的資料