Django rest framework源码分析(4)----版本 版本
新建一个工程Myproject和一个app名为api
(1)api/models.py
from django.db import models class UserInfo(models.Model): USER_TYPE = ( (1,'普通用户'), (2,'VIP'), (3,'SVIP') ) user_type = models.IntegerField(choices=USER_TYPE) username = models.CharField(max_length=32,unique=True) password = models.CharField(max_length=64) group = models.ForeignKey('UserGroup',on_delete=models.CASCADE) roles = models.ManyToManyField('Role') class UserToken(models.Model): user = models.OneToOneField('UserInfo',on_delete=models.CASCADE) token = models.CharField(max_length=64) class UserGroup(models.Model): title = models.CharField(max_length=32) class Role(models.Model): title = models.CharField(max_length=32)(2)Myproject/urls.py
from django.contrib import admin from django.urls import path,include urlpatterns = [ #path('admin/', admin.site.urls), path('api/',include('api.urls') ), ](3)api/urls.py
# api/urls.py from django.urls import path from .views import UserView urlpatterns = [ path('users/', UserView.as_view()), ](4)views.py
# api/views.py from django.shortcuts import render,HttpResponse from rest_framework.views import APIView from rest_framework.request import Request from rest_framework.versioning import QueryParameterVersioning class UserView(APIView): versioning_class = QueryParameterVersioning def get(self,request,*args,**kwargs): #获取版本 print(request.version) return HttpResponse('用户列表')(5)settings.py
#版本 REST_FRAMEWORK = { "DEFAULT_VERSION":'v1', #默认的版本 "ALLOWED_VERSIONS":['v1','v2'], #允许的版本 "VERSION_PARAM":'version' #GET方式url中参数的名字 ?version=xxx } 1.url中通过GET传参 QueryParameterVersioning用于去GET参数中取version :8000/api/users/?version=v2后台可以看到当前的版本
如果url中没有传版本参数,则显示默认的版本("DEFAULT_VERSION":'v1')
:8000/api/users/如果url传的版本超过settings中的允许范围则报错
:8000/api/users/?version=v3 2.在URLPATH中获取(1)修改api/urls.py
通常情况我门应该用URLPATH的方式,而不是用前面GET()传参方式
url里面通过正则表达式定义哪些版本,
# api/urls.py from django.urls import path,re_path from .views import UserView urlpatterns = [ re_path('(?P<version>[v1|v2]+)/users/', UserView.as_view()), ](2)views.py
URLPathVersioning:去url路径里面获取版本 # api/views.py from django.shortcuts import render,HttpResponse from rest_framework.views import APIView from rest_framework.request import Request from rest_framework.versioning import URLPathVersioning class UserView(APIView): versioning_class = URLPathVersioning def get(self,request,*args,**kwargs): #获取版本 print(request.version) return HttpResponse('用户列表')这个URLPathVersioning我们可以放到settings里面,全局配置,就不用写到views里面,每个类都要写一遍了
settings.py
# 版本 # REST_FRAMEWORK = { # "DEFAULT_VERSIONING_CLASS":"rest_framework.versioning.URLPathVersioning", # "DEFAULT_VERSION":'v1', #默认的版本 # "ALLOWED_VERSIONS":['v1','v2'], #允许的版本 # "VERSION_PARAM":'version' #get方式url中参数的名字 ?version=xxx # } #全局 REST_FRAMEWORK = { "DEFAULT_VERSIONING_CLASS":"rest_framework.versioning.URLPathVersioning", }修改views.py
# api/views.py from django.shortcuts import render,HttpResponse from rest_framework.views import APIView from rest_framework.request import Request class UserView(APIView): def get(self,request,*args,**kwargs): #获取版本 print(request.version) return HttpResponse('用户列表')浏览器访问地址
:8000/api/v1/users/然后后台拿到版本信息
3.反向解析访问的url(1)api/urls.py
添加name = 'api_user'
# api/urls.py from django.urls import path,re_path from .views import UserView urlpatterns = [ re_path('(?P<version>[v1|v2]+)/users/', UserView.as_view(),name = 'api_user'), ](2)views.py
# api/views.py from django.shortcuts import render,HttpResponse from rest_framework.views import APIView from rest_framework.request import Request class UserView(APIView): def get(self,request,*args,**kwargs): #获取版本 print(request.version) #获取处理版本的对象 print(request.versioning_scheme) #获取浏览器访问的url,reverse反向解析 #需要两个参数:viewname就是url中的别名,request=request是url中要传入的参数 #(?P<version>[v1|v2]+)/users/,这里本来需要传version的参数,但是version包含在request里面(源码里面可以看到),所有只需要request=request就可以 url_path = request.versioning_scheme.reverse(viewname='api_user',request=request) print(url_path) # self.dispatch return HttpResponse('用户列表')浏览器访问
:8000/api/v1/users/后台获取