在restful规范中要去,后端的API中需要体现版本。
drf框架中支持5种版本的设置。这里只介绍常用的2种。
URL的GET参数传递
[http://127.0.0.1:8000/users/?version=v1](http://127.0.0.1:8000/users/?version=v1)
QueryParameterVersioning
# app01/views.py
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import QueryParameterVersioning
# Create your views here.
class UserView(APIView):
versioning_class = QueryParameterVersioning
def get(self, request, *args, **kwargs):
print(request.version)
return Response({"code": 1000, "data": "xxx"})
def post(self, request, *args, **kwargs):
return Response({"code": 1000, "data": "xxx"})
全局设置
# settings.py
REST_FRAMEWORK = {
"DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.QueryParameterVersioning"
}
# app01/views.py
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
class UserView(APIView):
def get(self, request, *args, **kwargs):
print(request.version)
return Response({"code": 1000, "data": "xxx"})
def post(self, request, *args, **kwargs):
return Response({"code": 1000, "data": "xxx"})
其它设置
http://127.0.0.1:8000/users/?v=v1
# settings.py
REST_FRAMEWORK = {
"VERSION_PARAM": "v", # 参数名:默认为version(这里改为v)
"DEFAULT_VERSION": "v1", # 参数值:默认为v1
"ALLOWED_VERSIONS": ["v1", "v2", "v3"], # # 参数限制
"DEFAULT_VERSIONING_CLASS":"rest_framework.versioning.QueryParameterVersioning"
}
# app01/views.py
print(request.v)
源码执行流程
URL路径传递
URLPathVersioning
# drf/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('api/<version>/users/', views.UserView.as_view()),
]
# app01/views.py
from rest_framework.response import Response
from rest_framework.versioning import URLPathVersioning
# Create your views here.
class UserView(APIView):
versioning_class = URLPathVersioning
def get(self, request, *args, **kwargs):
print(request.version)
return Response({"code": 1000, "data": "xxx"})
def post(self, request, *args, **kwargs):
return Response({"code": 1000, "data": "xxx"})
全局设置
# settings.py
REST_FRAMEWORK = {
"DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.QueryParameterVersioning"
}
其它设置
http://127.0.0.1:8000/api/v1/users/
REST_FRAMEWORK = {
"VERSION_PARAM": "v", # 这里要和路径中的名字一致, path('api/<v>/users/', views.UserView.as_view()),
"DEFAULT_VERSION": "1",
"ALLOWED_VERSIONS": ["v1", "v2", "v3"],
"DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.URLPathVersioning"
}
# app01/views.py
print(request.version) # 必须是request.version
反向生成URL
使用的较少,作为了解
# app01/views.py
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
class UserView(APIView):
def get(self, request, *args, **kwargs):
print(request.version)
url1 = request.versioning_scheme.reverse('u1', args=args, request=request)
print(url1) # http://127.0.0.1:8000/api/v1/users/
url2 = request.versioning_scheme.reverse('u2', kwargs=kwargs, request=request)
print(url2) # http://127.0.0.1:8000/api/v1/users/2
return Response({"code": 1000, "data": "xxx"})
def post(self, request, *args, **kwargs):
return Response({"code": 1000, "data": "xxx"})