rest-framework source code parsing and custom components ---- version

version

  • the url parameter passing by GET
    customized version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
from rest_framework.views import APIView
from rest_framework.request import Request


class ParamVersion(object):
def determin_version(self,request):
version = request.query_params.get('version')
return version

UsersView class (APIView):
DEF GET (Self, Request, * args, ** kwargs):
# Request First, start with your own classes to take, if they are not a class, the class went native in the pick ,
# otherwise it will throw an exception
# Version = request._request.GET.get ( 'Version')
# Print (Version)
# Version = request.query_param.get ( 'Version') # and request._request.GET.get ( 'version') exactly the same as the value of
# Print Version
return HttpResponse ( 'user list')
  • Use the built-in class
1
2
3
4
5
6
from rest_framework.versioning import QueryParameterVersioning
class UsersView(APIView):
versioning_class = QueryParameterVersioning
def get(self,request,*args,**kwargs):
print(request.version)
return HttpResponse('用户列表')

Untitled-1-2018420213639

  • This 3 only needs to be set in the configuration file can be

  • Url parameter passing in the path (recommended)

1
2
3
4
5
6
REST_FRAMEWORK = {
'DEFAULT_VERSION_CLASS':'rest_framework.versioning.URLPathVersioning',
'DEFAULT_VERSION':'v1',
'ALLOWED_VERSIONS':['v1','v2'],
'VERSION_PARAM':'version',
}
1
2
3
4
class UsersView(APIView):
def get(self,request,*args,**kwargs):
print(request.version)
return HttpResponse('用户列表')
1
2
3
4
urlpatterns = [
# url(r'^users/',views.UsersView.as_view()),
url(r'^(?P<version>[v1|v2]+)/users/$',views.UsersView.as_view()),
]
  • Source Process
    • Returns the version is after packaging request, prior to certification and permission to do
    • BaseVersioning objects

Untitled-1-2018420213814

  • Which has a rest framework packages reverse (), to generate the reverse url, do not need to specify the version, automatically generated, in fact, the current version of the url, the bottom layer is essentially used Django own reverse () implementation.
. 1 
2
. 3
. 4
. 5
. 6
. 7
8 major Box   rest-framework source code and parsing custom components ---- version AN>
. 9
10
. 11
12 is
13 is
14
class UsersView(APIView):
def get(self,request,*args,**kwargs):
self.dispatch()
# 获取版本
print(request.version)
# 获取版本的对象
print(request.versioning_scheme)
# 内置的反向生成url,不需要指定版本,会自动生成,其实是当前url的版本
u1 = request.versioning_scheme.reverse(viewname='uuu',request=request)
print(u1)
# 使用Django内置的反向生成url,必须要指定版本
u2 = reverse(viewname='uuu',kwargs={'version':1})
print(u2)
return HttpResponse('用户列表')
  • 除了这些,版本的实现还有很多
    Untitled-1-2018420214715
总结
  • 使用 配置文件

    1
    2
    3
    4
    5
    6
    REST_FRAMEWORK = {
    'DEFAULT_VERSION_CLASS':'rest_framework.versioning.URLPathVersioning',
    'DEFAULT_VERSION':'v1',
    'ALLOWED_VERSIONS':['v1','v2'],
    'VERSION_PARAM':'version',
    }
  • 路由系统

1
2
3
4
urlpatterns = [
# url(r'^users/',views.UsersView.as_view()),
url(r'^(?P<version>[v1|v2]+)/users/$',views.UsersView.as_view(),name='uuu'),
]
1
2
3
4
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/$',include('myapp.urls'))
]
  • 视图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class UsersView(APIView):
def get(self,request,*args,**kwargs):
self.dispatch()
# 1.获取版本
print(request.version)
# 2.获取版本的对象
print(request.versioning_scheme)
# 3. 内置的反向生成url,不需要指定版本,会自动生成,其实是当前url的版本
u1 = request.versioning_scheme.reverse(viewname='uuu',request=request)
print(u1)
# 使用Django内置的反向生成url,必须要指定版本
u2 = reverse(viewname='uuu',kwargs={'version':1})
print(u2)
return HttpResponse('用户列表')
  • Source processes, and similar authentication source,
    • Matched to url, then in view to view, as_view run a parent () method returns a view function, and the view () function returns Lane dispatch () method, we started from the dispatch () method
    • First a good package request, and then perform the initial () implementation version and is certified, then the judge's permission, the back is the access frequency
    • Call determine_version () returns a tuple, there are two elements, respectively, and the processed version of the version of the object
    • Version is to get in determine_version () method, by versioning_class () method to get to URLPathVersioning object, the default is taken from the configuration file to, and permissions, throttle and other similar processes, but those are to get a list, and needs only to get a version of versioning_class it can also be set to view.

Source download

Guess you like

Origin www.cnblogs.com/lijianming180/p/12099798.html