Restful framework【第二篇】APIView

 


安装djangorestframework#

方式一:pip3 install djangorestframework

方式二:pycharm图形化界面安装

方式三:pycharm命令行下安装(装在当前工程所用的解释器下)

djangorestframework的APIView分析#

as_view方法#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@classmethod
   def as_view(cls, **initkwargs):
       """
       Store the original class on the view function.
 
       This allows us to discover information about the view when we do URL
       reverse lookups.  Used for breadcrumb generation.
       """
       if isinstance(getattr(cls, 'queryset', None), models.query.QuerySet):
           def force_evaluation():
               raise RuntimeError(
                   'Do not evaluate the `.queryset` attribute directly, '
                   'as the result will be cached and reused between requests. '
                   'Use `.all()` or call `.get_queryset()` instead.'
               )
           cls.queryset._fetch_all = force_evaluation
 
       view = super(APIView, cls).as_view(**initkwargs)
       view.cls = cls
       view.initkwargs = initkwargs
 
       # Note: session based authentication is explicitly CSRF validated,
       # all other authentication is CSRF exempt.
       return csrf_exempt(view)

dispatch#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def dispatch(self, request, *args, **kwargs):
        """
        `.dispatch()` is pretty much the same as Django's regular dispatch,
        but with extra hooks for startup, finalize, and exception handling.
        """
        self.args = args
        self.kwargs = kwargs
        request = self.initialize_request(request, *args, **kwargs)
        self.request = request
        self.headers = self.default_response_headers  # deprecate?
 
        try:
            self.initial(request, *args, **kwargs)
 
            # Get the appropriate handler method
            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
 
            response = handler(request, *args, **kwargs)
 
        except Exception as exc:
            response = self.handle_exception(exc)
 
        self.response = self.finalize_response(request, response, *args, **kwargs)
        return self.response

initialize_request#

1
2
3
4
5
6
7
8
9
10
11
12
13
def initialize_request(self, request, *args, **kwargs):
        """
        Returns the initial request object.
        """
        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
        )

initial方法(内部调用认证,权限和频率)#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def initial(self, request, *args, **kwargs):
       """
       Runs anything that needs to occur prior to calling the method handler.
       """
       self.format_kwarg = self.get_format_suffix(**kwargs)
 
       # Perform content negotiation and store the accepted info on the request
       neg = self.perform_content_negotiation(request)
       request.accepted_renderer, request.accepted_media_type = neg
 
       # Determine the API version, if versioning is in use.
       version, scheme = self.determine_version(request, *args, **kwargs)
       request.version, request.versioning_scheme = version, scheme
 
       # Ensure that the incoming request is permitted
       self.perform_authentication(request)
       self.check_permissions(request)
       self.check_throttles(request)

djangorestframework的Request对象简单介绍#

 

posted @   鲸鱼的海老大  阅读(157)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示
CONTENTS