1, version,
This is the 26th day of my participation in the August Text Challenge.More challenges in August
Thank you for meeting you. I’m Y Dazhuang
By Y Dazhuang Link: juejin.cn/user/756923… The copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.
🌊🌈
Part of the article and pictures from the Internet, if you have any questions please contact me
🌊🌈
The main thing is to configure the version information of settings.py, and then put the version information in the reference to either the URL or the request header….. Where where.
1.1 Custom Version
Custom: http://127.0.0.1:8000/api/users/? version=v2
class ParamVersion(object) :
def determine_version(self, request, *args, **kwargs) :
version = request.query_params.get('version')
return version
class UsersView(APIView) :
versioning_class = ParamVersion Note that this is a list, not a list
def get(self,request,*args,**kwargs) :
#version = request._request.GET.get('version')
#print(version)
# version = request.query_params.get('version')
# print(version)
print(request.version)
return HttpResponse('User List')
Copy the code
1.2 Using the Built-in version
# use:Configuration file: REST_FRAMEWORK = {'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning'.# Version information
"DEFAULT_VERSION": 'v1'.The default version number is None
"ALLOWED_VERSIONS": ['v1'.'v2'].# Version number to allow requests. Default is None
"VERSION_PARAM": 'version'.# identify the name of the version number parameter, default is 'version'} routes: urlpatterns = [# main by
url(r'^api/', include('api.urls')),
]
urlpatterns = [
# http://127.0.0.1:8000/api/v1/users/
# http://127.0.0.1:8000/api/v2/users/
url(r'^(? P
[v1|v2]+)/users/$'
, views.UsersView.as_view(),name='uuu'),]# views.py
class UsersView(APIView) :
permission_classes = []
def get(self, request, *args, **kwargs) :
# 1. Get version V1
print(request.version)
# 2. Obtain processing version of the object < rest_framework. Versioning. URLPathVersioning object at 0 x000001c3aa7d3e10 >
print(request.versioning_scheme)
# 3. Reverse generated URL (rest framework) http://127.0.0.1:8000/api/v1/users/
u1 = request.versioning_scheme.reverse(viewname='uuu', request=request)
print(u1)
# 4. Reverse generate URL/API /2/users/
u2 = reverse(viewname='uuu', kwargs={'version': 2})
print(u2)
return HttpResponse('User List')
Copy the code
Pay attention to
request.version
request.versioning_scheme
request.versioning_scheme.reverse(viewname='uuu', request=request)
reverse(viewname='uuu', kwargs={'version': 2})
Copy the code
1.3 pay attention to
To obtain the version number of a request, you can use request.version to obtain the version number.
The default version function is disabled, and Request. version returns None.
To enable version support, set DEFAULT_VERSIONING_CLASS in the configuration file
'DEFAULT_VERSIONING_CLASS' : 'rest_framework. Versioning. URLPathVersioning', # version informationCopy the code
Other optional configurations:
- DEFAULT_VERSION The default version number. The default value is None
- ALLOWED_VERSIONS Specifies the version number of allowed requests. The default value is None
- VERSION_PARAM Identifies the name of the version number parameter, which defaults to ‘version’
1.4 More version processing methods
1) AcceptHeaderVersioning
The Accept passed in the request header carries version version information
GET /bookings/ HTTP/1.1
Host: example.com
Accept: application/json; version=1.0
Copy the code
2) URLPathVersioning(recommended)
The URL path contains version information
urlpatterns = [
url( r'^(? P
(v1|v2))/bookings/$'
, bookings_list,name='bookings-list' ),
url( r'^(? P
(v1|v2))/bookings/(? P
[0-9]+)/$'
,bookings_detail,name='bookings-detail')]Copy the code
3) NamespaceVersioning
Namespace defined in
# bookings/urls.py
urlpatterns = [
url(r'^$', bookings_list, name='bookings-list'),
url(r'^(? P
[0-9]+)/$'
, bookings_detail, name='bookings-detail')]# urls.py
urlpatterns = [
url(r'^v1/bookings/', include('bookings.urls', namespace='v1')),
url(r'^v2/bookings/', include('bookings.urls', namespace='v2')))Copy the code
4) HostNameVersioning
Host domain names carry version information
GET /bookings/ HTTP/1.1
Host: v1.example.com
Accept: application/json
Copy the code
5) QueryParameterVersioning
The query string contains version information
GET /something/? version=0.1 HTTP/1.1
Host: example.com
Accept: application/json
Copy the code
The sample
REST_FRAMEWORK = {
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.QueryParameterVersioning'
}
class BookInfoSerializer(serializers.ModelSerializer) :
""" Book data serializer ""
class Meta:
model = BookInfo
fields = ('id'.'btitle'.'bpub_date'.'bread'.'bcomment')
class BookInfoSerializer2(serializers.ModelSerializer) :
""" Book data serializer ""
class Meta:
model = BookInfo
fields = ('id'.'btitle'.'bpub_date')
class BookDetailView(RetrieveAPIView) :
queryset = BookInfo.objects.all(a)def get_serializer_class(self) :
if self.request.version == '1.0':
return BookInfoSerializer
else:
return BookInfoSerializer2
# 127.0.0.1:8000 / books / 2 /
# 127.0.0.1:8000 / books / 2 /? Version = 1.0
Copy the code
1.5 Source Code Process
# 1. Dispatch method under APIView
def dispatch(self, request, *args, **kwargs) :
Copy the code
Self. Initial (request, *args, **kwargs)
def initial(self, request, *args, **kwargs) :
# Version information
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme
# Ensure that the incoming request is permitted
# implement authentication
self.perform_authentication(request)
# Permission judgment
self.check_permissions(request)
# current limiting
self.check_throttles(request)
Copy the code
Self. determine_version(request, *args, **kwargs)
def determine_version(self, request, *args, **kwargs) :
Copy the code