欢迎来到赛兔子家园

drf版本

在请求中携带版本号,便于后续API的更新迭代:

 

https://home.cnblogs.com/u/qingchengzi/v1/info
https://home.cnblogs.com/u/qingchengzi/v2/info

 

 

版本传递方式

1、URL的GET参数传递

http://127.0.0.1:8000/api/app02/user/?version=v1

urls.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2024/3/20 20:40'
# software: PyCharm

from django.urls import path

from .views import UserView


urlpatterns = [

    path("user/",UserView.as_view())

]

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):
        print(request.version) # GET参数传递
        return Response({"code":1000,"data":"xxx"})

运行结果:

2、URL路径传递

http://127.0.0.1:8000/api/app02/user/v1/

urls.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2024/3/20 20:40'
# software: PyCharm

from django.urls import path

from .views import UserView

urlpatterns = [

    path("user/<str:version>/", UserView.as_view())

]

views.py

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import URLPathVersioning


class UserView(APIView):
    versioning_class = URLPathVersioning # url传递

    def get(self, request,*args,**kwargs):
        print(request.version) 
        return Response({"code":1000,"data":"xxx"})

程序运行结果

3、请求头传递

请求headers中添加Accept : application/json;version=版本号

urls.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2024/3/20 20:40'
# software: PyCharm

from django.urls import path

from .views import UserView

urlpatterns = [
    path("user/", UserView.as_view())
]

views.py

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import AcceptHeaderVersioning


# Create your views here.

class UserView(APIView):
    versioning_class = AcceptHeaderVersioning # 请求头传递

    def get(self, request,*args,**kwargs):
        print(request.version)
        return Response({"code":1000,"data":"xxx"})

请求执行结果:

 全局配置

上述示例中,如果想要应用某种 版本的形式,需要在每个视图类中定义类变量。如果你项目比较大,需要些很多的视图类,在每一个类中都写一遍会比较麻烦,所有drf中也支持了全局配置。

# drf配置
REST_FRAMEWORK = {
    "UNAUTHENTICATED_USER": None,
    "UNAUTHENTICATED_TOKEN": None,
    "DEFAULT_AUTHENTICATION_CLASSES": [
        # "utils.ext.auth.QueryParamsAuthentication",
        # "utils.ext.auth.HeaderAuthentication",
        # "utils.ext.auth.BodyAuthentication", # 认证组件全局配置
        # "utils.ext.auth.NoAuthentication",
    ],
    # 限流类全局配置
    "DEFAULT_THROTTLE_CLASSES":[
        # "utils.ext.throttle.MyRateThrottle",
    ],
    # 限流频率全局配置
    "DEFAULT_THROTTLE_RATES":{
        "user": "5/m" ,
    },

    # 版本全局配置
    # 版本处理类的路径
    "DEFAULT_VERSIONING_CLASS":"from rest_framework.versioning import QueryParameterVersioning",
    "VERSION_PARAM": "version",  # URL参数传参时的key,例如:xxxx?version=v1
    "ALLOWED_VERSIONS": ["v1", "v2", "v3"],  # 限制支持的版本,None表示无限制
    "DEFAULT_VERSION": "v1",  # 默认版本
}

反向生成URL

urls.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2024/3/20 20:40'
# software: PyCharm

from django.urls import path

from .views import UserView

urlpatterns = [

    path("user/", UserView.as_view(),name="v1"),
    path("user/<int:pk>/",UserView.as_view(),name="v2")

]

 views.py

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.versioning import QueryParameterVersioning


class UserView(APIView):
    versioning_class = QueryParameterVersioning  #GET方式传递
  
    def get(self, request,*args,**kwargs):
        print(request.version)
        url1 = request.versioning_scheme.reverse("v1",request=request)
        print("url1",url1)
        url2 = request.versioning_scheme.reverse("v2",args=(11,),request=request)
        print("url2",url2)
        return Response({"code":1000,"data":"xxx"})

程序输出结果:

 

posted on 2024-03-20 21:18  赛兔子  阅读(8)  评论(0编辑  收藏  举报

导航