DRF版本组件

1.DRF版本组件配置类局部使用  

  局部配置:

    方式一:直接配置versioning_class=QueryParameterVersioning

    方式二:直接配置versioning_class=URLPathVersioning

    #也需要在settings.py中的REST_FRAMEWORK进行全局配置

    '''

      default_version=api_settings.DEFAULT_VERSION

      allowed_versions=api_settings.ALLOWED_VERSIONS

      version_param=api_settings.VERSION_PARAM

    '''

  

  settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'api.apps.ApiConfig',
    'rest_framework',
]
 
REST_FRAMEWORK = {
    #1版本组件
    'DEFAULT_VERSION':'v1',#默认的API版本
    'ALLOWED_VERSIONS':['v1','v2'],#允许的API版本
    'VERSION_PARAM':'version',#版本的key,默认的就是version(注意要和api接口中的key对应)
 
}
settings.py

   urls.py  

    drf主路由配置urls.py  

from django.conf.urls import url,include
urlpatterns = [
    url(r'^api/', include('api.urls')),
]
drf主路由配置urls.py:

    api主路由配置urls.py:

from django.conf.urls import url
from api import views
 
urlpatterns = [
    url(r'^(?P<version>v[\d+])/users/',views.UsersView.as_view(),name='users'),
]
 
api主路由配置urls.py:

  views.py:  

from django.shortcuts import render, HttpResponse
from rest_framework.views import APIView
from rest_framework.versioning import BaseVersioning, QueryParameterVersioning, URLPathVersioning
from django.urls import reverse
 
 
class UsersView(APIView):
    # API版本组件(局部配置)----返回结果都是一样
    # 也需要在settings.py中的REST_FRAMEWORK进行全局配置
    '''
        default_version = api_settings.DEFAULT_VERSION
        allowed_versions = api_settings.ALLOWED_VERSIONS
        version_param = api_settings.VERSION_PARAM
    '''
    # (1)当版本号以查询参数形式传递时:比如:http://127.0.0.1:8000/api/users/?version=v1
    # (推荐第2种方式,此方式url(r'^/users/',views.UsersView.as_view(),name='users'),)
    # versioning_class = QueryParameterVersioning
 
    # (2)当版本号以路径urlpath形式传递时:比如:http://127.0.0.1:8000/api/v1/users/
    # (注意在urls.py中使用正则匹配url(r'^(?P<version>v[\d+])/users/',views.UsersView.as_view(),name='users'),)
    versioning_class = URLPathVersioning
 
    def get(self, request, *args, **kwargs):
        print(request.version)  # 获取版本号
        print(request.versioning_scheme)  # 获取到版本号处理的类对象,封装了reverse方法
        print(request.versioning_scheme.reverse(viewname='users', request=request))  # 反向解析url
        # print(reverse(viewname='users',kwargs={'version':request.version}))#django原生解析,必须指定参数
 
        return HttpResponse('get_users')
 
    def post(self, request, *args, **kwargs):
        return HttpResponse('post_user')
views.py:

 

2.DRF版本组件配置类推荐全局配置  

  一般在版本组件中推荐使用URLPathVersioning类进行版本控制

  

  settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'api.apps.ApiConfig',
    'rest_framework',
]
 
REST_FRAMEWORK = {
    #版本组件(全局配置)----针对所有的继承APIView的类,在认证+权限+节流之前
    'DEFAULT_VERSIONING_CLASS':'rest_framework.versioning.URLPathVersioning',#设置版本组件类---#http://127.0.0.1:8000/api/v1/users/
    'DEFAULT_VERSION':'v1',#默认的API版本
    'ALLOWED_VERSIONS':['v1','v2'],#允许的API版本
    'VERSION_PARAM':'version',#版本的key,默认的就是version(注意要和api接口中的key对应)
 
}
 
settings.py

 urls.py  

    drf主路由配置urls.py

from django.conf.urls import url,include
urlpatterns = [
    url(r'^api/', include('api.urls')),
]
drf主路由配置urls.py:

    api主路由配置urls.py:  

from django.conf.urls import url
from api import views
#http://127.0.0.1:8000/api/v1/users/
urlpatterns = [
    url(r'^(?P<version>v[\d+])/users/',views.UsersView.as_view(),name='users'),
]
api主路由配置urls.py:

  views.py:

from django.shortcuts import render, HttpResponse
from rest_framework.views import APIView
from django.urls import reverse
 
 
class UsersView(APIView):
    
    def get(self, request, *args, **kwargs):
        print(request.version)  # 获取版本号
        print(request.versioning_scheme)  # 获取到版本号处理的类对象,封装了reverse方法
        print(request.versioning_scheme.reverse(viewname='users', request=request))  # 反向解析url
        # print(reverse(viewname='users',kwargs={'version':request.version}))#django原生解析,必须指定参数
 
        return HttpResponse('get_users')
 
    def post(self, request, *args, **kwargs):
        return HttpResponse('post_user')
 
views.py:

 

posted @ 2019-09-23 16:56  笑得好美  阅读(241)  评论(0编辑  收藏  举报