在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

  1. # app01/views.py
  2. from rest_framework.views import APIView
  3. from rest_framework.response import Response
  4. from rest_framework.versioning import QueryParameterVersioning
  5. # Create your views here.
  6. class UserView(APIView):
  7. versioning_class = QueryParameterVersioning
  8. def get(self, request, *args, **kwargs):
  9. print(request.version)
  10. return Response({"code": 1000, "data": "xxx"})
  11. def post(self, request, *args, **kwargs):
  12. return Response({"code": 1000, "data": "xxx"})

全局设置

  1. # settings.py
  2. REST_FRAMEWORK = {
  3. "DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.QueryParameterVersioning"
  4. }
  1. # app01/views.py
  2. from rest_framework.views import APIView
  3. from rest_framework.response import Response
  4. # Create your views here.
  5. class UserView(APIView):
  6. def get(self, request, *args, **kwargs):
  7. print(request.version)
  8. return Response({"code": 1000, "data": "xxx"})
  9. def post(self, request, *args, **kwargs):
  10. return Response({"code": 1000, "data": "xxx"})

其它设置

http://127.0.0.1:8000/users/?v=v1

  1. # settings.py
  2. REST_FRAMEWORK = {
  3. "VERSION_PARAM": "v", # 参数名:默认为version(这里改为v)
  4. "DEFAULT_VERSION": "v1", # 参数值:默认为v1
  5. "ALLOWED_VERSIONS": ["v1", "v2", "v3"], # # 参数限制
  6. "DEFAULT_VERSIONING_CLASS":"rest_framework.versioning.QueryParameterVersioning"
  7. }
  1. # app01/views.py
  2. print(request.v)

源码执行流程image-20210820105543193.png

URL路径传递

URLPathVersioning

  1. # drf/urls.py
  2. urlpatterns = [
  3. path('admin/', admin.site.urls),
  4. path('api/<version>/users/', views.UserView.as_view()),
  5. ]
  1. # app01/views.py
  2. from rest_framework.response import Response
  3. from rest_framework.versioning import URLPathVersioning
  4. # Create your views here.
  5. class UserView(APIView):
  6. versioning_class = URLPathVersioning
  7. def get(self, request, *args, **kwargs):
  8. print(request.version)
  9. return Response({"code": 1000, "data": "xxx"})
  10. def post(self, request, *args, **kwargs):
  11. return Response({"code": 1000, "data": "xxx"})

全局设置

  1. # settings.py
  2. REST_FRAMEWORK = {
  3. "DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.QueryParameterVersioning"
  4. }

其它设置

http://127.0.0.1:8000/api/v1/users/

  1. REST_FRAMEWORK = {
  2. "VERSION_PARAM": "v", # 这里要和路径中的名字一致, path('api/<v>/users/', views.UserView.as_view()),
  3. "DEFAULT_VERSION": "1",
  4. "ALLOWED_VERSIONS": ["v1", "v2", "v3"],
  5. "DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.URLPathVersioning"
  6. }
  1. # app01/views.py
  2. print(request.version) # 必须是request.version

反向生成URL

使用的较少,作为了解

  1. # app01/views.py
  2. from rest_framework.views import APIView
  3. from rest_framework.response import Response
  4. # Create your views here.
  5. class UserView(APIView):
  6. def get(self, request, *args, **kwargs):
  7. print(request.version)
  8. url1 = request.versioning_scheme.reverse('u1', args=args, request=request)
  9. print(url1) # http://127.0.0.1:8000/api/v1/users/
  10. url2 = request.versioning_scheme.reverse('u2', kwargs=kwargs, request=request)
  11. print(url2) # http://127.0.0.1:8000/api/v1/users/2
  12. return Response({"code": 1000, "data": "xxx"})
  13. def post(self, request, *args, **kwargs):
  14. return Response({"code": 1000, "data": "xxx"})