drf总流程链接
https://www.cnblogs.com/daviddd/p/11918405.html
drf之版本控制
'''
承接rest_framework主流程5.1内容,
视图函数在rest_framework的views.py文件中
drf版本控制,需要APIView基类的静态属性
'''
class APIView(View):
# settings配置文件
versioning_class = api_settings.DEFAULT_VERSIONING_CLASS
settings = api_settings
schema = DefaultSchema()
def initial(self, request, *args, **kwargs):
# 5.1,版本信息
# Determine the API version, if versioning is in use.
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme
# 5.12,执行determine_version函数
def determine_version(self, request, *args, **kwargs):
"""
If versioning is being used, then determine any API version for the
incoming request. Returns a two-tuple of (version, versioning_scheme)
"""
if self.versioning_class is None:
return (None, None)
# 5.13
'''
根据settings配置文件中或者自定义的versioning_class,实例化对象URLPathVersioning()
from rest_framework.versioning import URLPathVersioning
'''
scheme = self.versioning_class()
# 5.14
'''
执行类URLPathVersioning中的 determine_version()方法,
在这里获取到scheme就是api_settings.DEFAULT_VERSIONING_CLASS,
系统默认版本控制类,那scheme.determine_version(request, *args, kwargs)
就是该类下的一个方法查看rest_framework库的一个自带类中的determine_version方法
'''
return (scheme.determine_version(request, *args, **kwargs), scheme)
# 5.15,
'''
这是URLPathVersioning类中的determine_version()方法
返回的是版本号。于是version, scheme分别是版本号和一个版本控制类,
并分别赋值给request对象的version, versioning_scheme属性
'''
def determine_version(self, request, *args, **kwargs):
version = kwargs.get(self.version_param, self.default_version)
if version is None:
version = self.default_version
if not self.is_allowed_version(version):
raise exceptions.NotFound(self.invalid_version_message)
return version