目录

版本

新建一个工程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
http://127.0.0.1:8000/api/users/?version=v2

后台可以看到当前的版本

如果url中没有传版本参数,则显示默认的版本("DEFAULT_VERSION":'v1')

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

如果url传的版本超过settings中的允许范围则报错

http://127.0.0.1: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('用户列表')

浏览器访问地址

http://127.0.0.1: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('用户列表')

浏览器访问

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

后台获取

源码流程

(1)dispatch

 def dispatch(self, request, *args, **kwargs):
"""
`.dispatch()` is pretty much the same as Django's regular dispatch,
but with extra hooks for startup, finalize, and exception handling.
"""
self.args = args
self.kwargs = kwargs
#对原始request进行加工,丰富了一些功能
#Request(
# request,
# parsers=self.get_parsers(),
# authenticators=self.get_authenticators(),
# negotiator=self.get_content_negotiator(),
# parser_context=parser_context
# )
#request(原始request,[BasicAuthentications对象,])
#获取原生request,request._request
#获取认证类的对象,request.authticators
#1.封装request
request = self.initialize_request(request, *args, **kwargs)
self.request = request
self.headers = self.default_response_headers # deprecate? try:
#2.认证
self.initial(request, *args, **kwargs) # Get the appropriate handler method
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(),
self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed response = handler(request, *args, **kwargs) except Exception as exc:
response = self.handle_exception(exc) self.response = self.finalize_response(request, response, *args, **kwargs)
return self.response

(2)initial

    def initial(self, request, *args, **kwargs):
"""
Runs anything that needs to occur prior to calling the method handler.
"""
self.format_kwarg = self.get_format_suffix(**kwargs) # Perform content negotiation and store the accepted info on the request
neg = self.perform_content_negotiation(request)
request.accepted_renderer, request.accepted_media_type = neg # Determine the API version, if versioning is in use.
#request.version获取版本信息
#request.versioning_scheme获取处理版本你的对象
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme # Ensure that the incoming request is permitted
#4.实现认证
self.perform_authentication(request)
#5.权限判断
self.check_permissions(request)
#6.控制访问频率
self.check_throttles(request)

(3)determine_version

 def determine_version(self, request, *args, **kwargs):
"""
If versioning is being used, then determine any API version for the
incoming request. Returns a two-tuple of (version, versioning_scheme)
"""
if self.versioning_class is None:
return (None, None)
scheme = self.versioning_class()
return (scheme.determine_version(request, *args, **kwargs), scheme)

(4)versioning_class

URLPathVersioning源码

class URLPathVersioning(BaseVersioning):
"""
To the client this is the same style as `NamespaceVersioning`.
The difference is in the backend - this implementation uses
Django's URL keyword arguments to determine the version. An example URL conf for two views that accept two different versions. urlpatterns = [
url(r'^(?P<version>[v1|v2]+)/users/$', users_list, name='users-list'),
url(r'^(?P<version>[v1|v2]+)/users/(?P<pk>[0-9]+)/$', users_detail, name='users-detail')
] GET /1.0/something/ HTTP/1.1
Host: example.com
Accept: application/json
"""
invalid_version_message = _('Invalid version in URL path.') def determine_version(self, request, *args, **kwargs):
version = kwargs.get(self.version_param, self.default_version)
if not self.is_allowed_version(version):
raise exceptions.NotFound(self.invalid_version_message)
return version def reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra):
if request.version is not None:
kwargs = {} if (kwargs is None) else kwargs
kwargs[self.version_param] = request.version return super(URLPathVersioning, self).reverse(
viewname, args, kwargs, request, format, **extra
)

可以看到

(1)url配置

(2)determine_version

里面有个is_allowed_version,点进去可以看到一些基本参数 (继承BaseVersioning基类)

class BaseVersioning(object):
#默认的版本
default_version = api_settings.DEFAULT_VERSION
#允许的版本
allowed_versions = api_settings.ALLOWED_VERSIONS
#默认参数(是version,比如你可以自定义为v)
version_param = api_settings.VERSION_PARAM def determine_version(self, request, *args, **kwargs):
msg = '{cls}.determine_version() must be implemented.'
raise NotImplementedError(msg.format(
cls=self.__class__.__name__
)) def reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra):
return _reverse(viewname, args, kwargs, request, format, **extra) def is_allowed_version(self, version):
if not self.allowed_versions:
return True
return ((version is not None and version == self.default_version) or
(version in self.allowed_versions))

Django rest framework(4)----版本的更多相关文章

  1. Django Rest framework 之 版本

    RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...

  2. Django REST framework之版本,解释器,序列化

    1 版本 2 解释器 3.序列化 1 版本 通过?后面传版本号有两种方法: 方法一 from django.shortcuts import render from rest_framework.vi ...

  3. Django rest framework 之版本

    一.通过 QueryParameterVersioning 获取版本 通过 QueryParameterVersioning 从 get 请求中获取版本信息: 1.新建 app,名为 api,Proj ...

  4. Django Rest framework 之 序列化

    RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...

  5. Django Rest framework 之 解析器

    RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...

  6. Django Rest framework 之 节流

    RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...

  7. Django Rest framework 之 权限

    django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) django res ...

  8. Django Rest framework 之 认证

    django rest framework 官网 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest fra ...

  9. Django Rest framework 之 视图

    RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...

  10. Django Rest framework 之 分页

    RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...

随机推荐

  1. centos搭建免费的ssl证书,大部分浏览器均支持!(let’s encrypt 的使用记录)

    安装certbot wget https://dl.eff.org/certbot-auto chmod a+x certbot-auto 然后就是通过这个脚本获取证书,安装前先将NGINX 停一下. ...

  2. Win8Metro(C#)数字图像处理--2.12Sobel边缘检测

    原文:Win8Metro(C#)数字图像处理--2.12Sobel边缘检测  [函数名称] 图像Sobel边缘检测函数SobelEdgeProcess(WriteableBitmap src) [ ...

  3. UWP 浏览本地图片及对图片的裁剪

    原文:UWP 浏览本地图片及对图片的裁剪 1.前言 准备给我的校园助手客户端添加一个修改头像的功能,但是查了好多资料都没有找到裁剪图片的简单的方法,最后才找到这个使用Launcher调用系统组件的简单 ...

  4. WCF nginx反向代理遇到的问题

    正常配置了nginx反向代理,其他java站点什么的都正常,就wcf总是失败.始终会跑如下异常: 由于 AddressFilter 在 EndpointDispatcher 不匹配,To 为“http ...

  5. 用C#修改系统区域和语言设置

    原文:用C#修改系统区域和语言设置 这几天做项目,因为客户机的系统不同,发现客户机的区域和语言设置也不尽相同,导致程序运行时根据时间判断的很多属性和方法都出现各种各样的千奇百怪的问题. 修改程序太费时 ...

  6. UWP-MSDN文档分类

    原文:UWP-MSDN文档分类 UWP学习目录整理 0x00 可以忽略的废话 10月6号靠着半听半猜和文字直播的补充看完了微软的秋季新品发布会,信仰充值成功,对UWP的开发十分感兴趣,打算后面找时间学 ...

  7. 机器学习Machine Learning(ML)

    什么是机器学习 定义 对于某个任务T和表现的衡量P,当计算机程序在该任务T的表现上,经过P的衡量,随着经验E而增长,称计算机能够通过经验E来学习该任务.(Tom Mitchell) 举例而言,在跳棋游 ...

  8. 初涉Delphi Socket编程

    不是第一次接触socket编程了,但以前都是看别人的依葫芦画瓢,也不知道具体的原理. 新的项目,有了新的开始,同时也需要有新的认识. Delphi 中带有两套TCP Socket组件: Indy So ...

  9. Wiki上的C++哲学

    Philosophy[edit] Throughout C++'s life, its development and evolution has been informally governed b ...

  10. XP下安装ubuntu

    一,环境说明 dell vostro 1400笔记本,winxp sp3操作系统,ubuntu-9.10-desktop-i386.iso 写这篇随笔的时候我用的已经是ubuntu了. 我是在我的移动 ...