第一版:
from rest_framework.versioning import BaseVersioning class ParamVersion(object): def determine_version(self, *args, **kwargs): version = request.query_params.get('version') return version class UserView(APIView): versioning_class = ParamVersion #加上版本控制类 .... .....
第二版:
from rest_framework.versioning import QueryParameterVersioning class UserView(APIView): versioning_class = QueryParameterVersioning#加上版本控制类 .... .....
全局设置:
"DEFAULT_VERSION": "v1", #默认允许版本 "ALLOWED_VAERSION": ['v1','v2'], #允许访问版本 v1和v2 "VERSION_PARAM": "version", #传参关键字 例:http://www.t.com/?version=v1
urls传参:
#例:http://www.t.com/api/v1/users/
url(r'^(?P<version>[v1|v2]+)'/users/$)
from rest_framework.versioning import URLPathVersioning
class UserView(APIView):
versioning_class =URLPathVersioning #加上版本控制类
....
.....
全局设置:
第三版:
"DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.URLPathVersioning"
#使用request获取
print(request.version)