#判断是否能访问当前版本 defis_allowed_version(self, version): ifnot self.allowed_versions: returnTrue #version存在并且等于默认版本或者version在允许访问的版本中 #返回True or Fasle return ((version isnotNoneand version == self.default_version) or (version in self.allowed_versions))
# 在accept请求头中配置版本信息 # accept代表希望返回的数据类型 可以携带版本信息 # Accept: application/json; version=1.0 classAcceptHeaderVersioning(BaseVersioning): ... pass # 在url上携带版本信息 # url(r'^(?P<version>[v1|v2]+)/users/$',users_list,name='users-list') classURLPathVersioning(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.')
# views.py from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.versioning import URLPathVersioning
# views.py from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.versioning import NamespaceVersioning