Django RESTFrame版本控制问题
1.Django 的rest_framework组件,提供了5中控制版本的方法(基于django路由系统的namespace暂未列出):
这5个类都是继承自:BaseVersioning
1 from rest_framework.versioning import QueryParameterVersioning 2 from rest_framework.versioning import URLPathVersioning 3 from rest_framework.versioning import AcceptHeaderVersioning 4 from rest_framework.versioning import HostNameVersioning 5 from rest_framework.versioning import NamespaceVersioning
1 class BaseVersioning(object): 2 default_version = api_settings.DEFAULT_VERSION 3 allowed_versions = api_settings.ALLOWED_VERSIONS 4 version_param = api_settings.VERSION_PARAM 5 6 def determine_version(self, request, *args, **kwargs): 7 msg = '{cls}.determine_version() must be implemented.' 8 raise NotImplementedError(msg.format( 9 cls=self.__class__.__name__ 10 )) 11 12 def reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra): 13 return _reverse(viewname, args, kwargs, request, format, **extra) 14 15 def is_allowed_version(self, version): 16 if not self.allowed_versions: 17 return True 18 return ((version is not None and version == self.default_version) or 19 (version in self.allowed_versions))
用法:
settings.py
REST_FRAMEWORK = { 'DEFAULT_VERSION': 'v1', # 默认版本 'ALLOWED_VERSIONS': ['v1', 'v2'], # 允许的版本 'VERSION_PARAM': 'version' # URL中获取值的key }
url.py
1 # 版本控制的方法, 2 url(r'^test/', views.TestView.as_view(),name='test'), # 基于url的get方法的版本控制 3 url(r'^(?P<version>[v1|v2]+)/test/$', views.TestView.as_view(),name='test'), # 版本控制,基于url的正则方式 4 url(r'^test/', views.TestView.as_view(), name='test'), # 版本控制,基于accept 请求头方式 5 url(r'^test/', views.TestView.as_view(), name='test'), # 版本控制,基于accept 请求头方式
views.py
from rest_framework.versioning import QueryParameterVersioning from rest_framework.versioning import URLPathVersioning from rest_framework.versioning import AcceptHeaderVersioning from rest_framework.versioning import HostNameVersioning from rest_framework.versioning import NamespaceVersioning ''' # 1.版本控制,基于url的get传参方式 class TestView(APIView): versioning_class = QueryParameterVersioning def get(self,request,*args,**kwargs): print(request.version) # 获取版本 self.dispatch print(request.versioning_scheme) # 获取版本管理的类 reverse_url = request.versioning_scheme.reverse('test',request=request) print(reverse_url) return Response('GET请求,响应内容') ''' ''' # 2.版本控制,基于url的正则方式,例如 /v1/test/ class TestView(APIView): versioning_class = URLPathVersioning def get(self,request,*args,**kwargs): print(request.version) # 获取版本 print(request.versioning_scheme) # 获取版本管理的类 reverse_url = request.versioning_scheme.reverse('test',request=request) print(reverse_url) return Response('GET请求,响应内容') ''' ''' # 3.版本控制,基于accept 请求头方式方式,例如 如:Accept: application/json; version=v1 , 需要请求头中写入,用postman来测试,在heders中加入:Accept:application/json;version=v1 class TestView(APIView): versioning_class = AcceptHeaderVersioning def get(self,request,*args,**kwargs): print(request.version) # 获取版本 print(request.versioning_scheme) # 获取版本管理的类 self.dispatch reverse_url = request.versioning_scheme.reverse('test',request=request) print(reverse_url) return Response('GET请求,响应内容') ''' ''' # 基于主机名方法 例如:v1.example.com ,需要在settings配置ALLOWED_HOSTS = ['*'] class TestView(APIView): versioning_class = HostNameVersioning def get(self, request, *args, **kwargs): # 获取版本 print(request.version) # 获取版本管理的类 print(request.versioning_scheme) # 反向生成URL reverse_url = request.versioning_scheme.reverse('test', request=request) print(reverse_url) return Response('GET请求,响应内容') def post(self, request, *args, **kwargs): return Response('POST请求,响应内容') def put(self, request, *args, **kwargs): return Response('PUT请求,响应内容') '''