第一种分页pagenumberpagination
基本使用
(1)urls.py
1
2
3
|
urlpatterns = [ re_path( '(?p<version>[v1|v2]+)/page1/' , pager1view.as_view(),) #分页1 ] |
(2)api/utils/serializers/pager.py
(3)views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from api.utils.serializsers.pager import pagerserialiser from rest_framework.response import response from rest_framework.pagination import pagenumberpagination class pager1view(apiview): def get( self ,request, * args, * * kwargs): #获取所有数据 roles = models.role.objects. all () #创建分页对象 pg = pagenumberpagination() #获取分页的数据 page_roles = pg.paginate_queryset(queryset = roles,request = request,view = self ) #对数据进行序列化 ser = pagerserialiser(instance = page_roles,many = true) return response(ser.data) |
(4)settings配置
1
2
3
4
|
rest_framework = { #分页 "page_size" : 2 #每页显示多少个 } |
自定义分页类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#自定义分页类 class mypagenumberpagination(pagenumberpagination): #每页显示多少个 page_size = 3 #默认每页显示3个,可以通过传入pager1/?page=2&size=4,改变默认每页显示的个数 page_size_query_param = "size" #最大页数不超过10 max_page_size = 10 #获取页码数的 page_query_param = "page" class pager1view(apiview): def get( self ,request, * args, * * kwargs): #获取所有数据 roles = models.role.objects. all () #创建分页对象,这里是自定义的mypagenumberpagination pg = mypagenumberpagination() #获取分页的数据 page_roles = pg.paginate_queryset(queryset = roles,request = request,view = self ) #对数据进行序列化 ser = pagerserialiser(instance = page_roles,many = true) return response(ser.data) |
第二种分页 limitoffsetpagination
自定义
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#自定义分页类2 class mylimitoffsetpagination(limitoffsetpagination): #默认显示的个数 default_limit = 2 #当前的位置 offset_query_param = "offset" #通过limit改变默认显示的个数 limit_query_param = "limit" #一页最多显示的个数 max_limit = 10 class pager1view(apiview): def get( self ,request, * args, * * kwargs): #获取所有数据 roles = models.role.objects. all () #创建分页对象 pg = mylimitoffsetpagination() #获取分页的数据 page_roles = pg.paginate_queryset(queryset = roles,request = request,view = self ) #对数据进行序列化 ser = pagerserialiser(instance = page_roles,many = true) return response(ser.data) |
返回的时候可以用get_paginated_response方法
自带上一页下一页
第三种分页cursorpagination
加密分页方式,只能通过点“上一页”和下一页访问数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#自定义分页类3 (加密分页) class mycursorpagination(cursorpagination): cursor_query_param = "cursor" page_size = 2 #每页显示2个数据 ordering = 'id' #排序 page_size_query_param = none max_page_size = none class pager1view(apiview): def get( self ,request, * args, * * kwargs): #获取所有数据 roles = models.role.objects. all () #创建分页对象 pg = mycursorpagination() #获取分页的数据 page_roles = pg.paginate_queryset(queryset = roles,request = request,view = self ) #对数据进行序列化 ser = pagerserialiser(instance = page_roles,many = true) # return response(ser.data) return pg.get_paginated_response(ser.data) |
代码
版本、解析器、序列化和分页
1
2
3
4
5
6
7
8
9
|
# myproject2/urls.py from django.contrib import admin from django.urls import path,include urlpatterns = [ #path('admin/', admin.site.urls), path( 'api/' ,include( 'api.urls' ) ), ] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# api/urls.py from django.urls import path,re_path from .views import userview,paserview,rolesview,userinfoview,groupview,usergroupview from .views import pager1view urlpatterns = [ re_path( '(?p<version>[v1|v2]+)/users/' , userview.as_view(),name = 'api_user' ), #版本 path( 'paser/' , paserview.as_view(),), #解析 re_path( '(?p<version>[v1|v2]+)/roles/' , rolesview.as_view()), #序列化 re_path( '(?p<version>[v1|v2]+)/info/' , userinfoview.as_view()), #序列化 re_path( '(?p<version>[v1|v2]+)/group/(?p<pk>\d+)/' , groupview.as_view(),name = 'gp' ), #序列化生成url re_path( '(?p<version>[v1|v2]+)/usergroup/' , usergroupview.as_view(),), #序列化做验证 re_path( '(?p<version>[v1|v2]+)/pager1/' , pager1view.as_view(),) #分页1 ] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# 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 ) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
# api/views.py import json from django.shortcuts import render,httpresponse from rest_framework.views import apiview from rest_framework.request import request from rest_framework.versioning import urlpathversioning from . import models ##########################################版本和解析器##################################################### 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( '用户列表' ) # from rest_framework.parsers import jsonparser,formparser class paserview(apiview): '''解析''' # parser_classes = [jsonparser,formparser,] #jsonparser:表示只能解析content-type:application/json的头 #formparser:表示只能解析content-type:application/x-www-form-urlencoded的头 def post( self ,request, * args, * * kwargs): #获取解析后的结果 print (request.data) return httpresponse( 'paser' ) ###########################################序列化########################################################### from rest_framework import serializers #要先写一个序列化的类 class rolesserializer(serializers.serializer): #role表里面的字段id和title序列化 id = serializers.integerfield() title = serializers.charfield() class rolesview(apiview): def get( self ,request, * args, * * kwargs): # 方式一:对于[obj,obj,obj] # (queryset) # roles = models.role.objects.all() # 序列化,两个参数,instance:queryset 如果有多个值,就需要加 mangy=true # ser = rolesserializer(instance=roles,many=true) # 转成json格式,ensure_ascii=false表示显示中文,默认为true # ret = json.dumps(ser.data,ensure_ascii=false) # 方式二: role = models.role.objects. all ().first() ser = rolesserializer(instance = role, many = false) ret = json.dumps(ser.data, ensure_ascii = false) return httpresponse(ret) # class userinfoserializer(serializers.serializer): # '''序列化用户的信息''' # #user_type是choices(1,2,3),显示全称的方法用source # type = serializers.charfield(source="get_user_type_display") # username = serializers.charfield() # password = serializers.charfield() # #group.title:组的名字 # group = serializers.charfield(source="group.title") # #serializermethodfield(),表示自定义显示 # #然后写一个自定义的方法 # rls = serializers.serializermethodfield() # # def get_rls(self,row): # #获取用户所有的角色 # role_obj_list = row.roles.all() # ret = [] # #获取角色的id和名字 # #以字典的键值对方式显示 # for item in role_obj_list: # ret.append({"id":item.id,"title":item.title}) # return ret # class userinfoserializer(serializers.modelserializer): # type = serializers.charfield(source="get_user_type_display") # group = serializers.charfield(source="group.title") # rls = serializers.serializermethodfield() # # def get_rls(self, row): # # 获取用户所有的角色 # role_obj_list = row.roles.all() # ret = [] # # 获取角色的id和名字 # # 以字典的键值对方式显示 # for item in role_obj_list: # ret.append({"id": item.id, "title": item.title}) # return ret # # class meta: # model = models.userinfo # fields = ['id','username','password','type','group','rls'] # class userinfoserializer(serializers.modelserializer): # class meta: # model = models.userinfo # #fields = "__all__" # fields = ['id','username','password','group','roles'] # #表示连表的深度 # depth = 1 class userinfoserializer(serializers.modelserializer): group = serializers.hyperlinkedidentityfield(view_name = 'gp' ,lookup_field = 'group_id' ,lookup_url_kwarg = 'pk' ) class meta: model = models.userinfo #fields = "__all__" fields = [ 'id' , 'username' , 'password' , 'group' , 'roles' ] #表示连表的深度 depth = 0 class userinfoview(apiview): '''用户的信息''' def get( self ,request, * args, * * kwargs): users = models.userinfo.objects. all () #这里必须要传参数context={'request':request} ser = userinfoserializer(instance = users,many = true,context = { 'request' :request}) ret = json.dumps(ser.data,ensure_ascii = false) return httpresponse(ret) class groupserializer(serializers.modelserializer): class meta: model = models.usergroup fields = "__all__" class groupview(apiview): def get( self ,request, * args, * * kwargs): pk = kwargs.get( 'pk' ) obj = models.usergroup.objects. filter (pk = pk).first() ser = groupserializer(instance = obj,many = false) ret = json.dumps(ser.data,ensure_ascii = false) return httpresponse(ret) ####################################序列化之用户请求数据验证验证#################################### #自定义验证规则 class groupvalidation( object ): def __init__( self ,base): self .base = base def __call__( self , value): if not value.startswith( self .base): message = "标题必须以%s为开头" % self .base raise serializers.validationerror(message) class usergroupserializer(serializers.serializer): title = serializers.charfield(validators = [groupvalidation( '以我开头' ),]) class usergroupview(apiview): def post( self ,request, * args, * * kwargs): ser = usergroupserializer(data = request.data) if ser.is_valid(): print (ser.validated_data[ 'title' ]) else : print (ser.errors) return httpresponse( "用户提交数据验证" ) ##################################################分页################################################### from api.utils.serializsers.pager import pagerserialiser from rest_framework.response import response from rest_framework.pagination import pagenumberpagination,limitoffsetpagination,cursorpagination # #自定义分页类1 # class mypagenumberpagination(pagenumberpagination): # #每页显示多少个 # page_size = 3 # #默认每页显示3个,可以通过传入pager1/?page=2&size=4,改变默认每页显示的个数 # page_size_query_param = "size" # #最大页数不超过10 # max_page_size = 10 # #获取页码数的 # page_query_param = "page" #自定义分页类2 class mylimitoffsetpagination(limitoffsetpagination): #默认显示的个数 default_limit = 2 #当前的位置 offset_query_param = "offset" #通过limit改变默认显示的个数 limit_query_param = "limit" #一页最多显示的个数 max_limit = 10 #自定义分页类3 (加密分页) class mycursorpagination(cursorpagination): cursor_query_param = "cursor" page_size = 2 #每页显示2个数据 ordering = 'id' #排序 page_size_query_param = none max_page_size = none class pager1view(apiview): def get( self ,request, * args, * * kwargs): #获取所有数据 roles = models.role.objects. all () #创建分页对象 pg = mycursorpagination() #获取分页的数据 page_roles = pg.paginate_queryset(queryset = roles,request = request,view = self ) #对数据进行序列化 ser = pagerserialiser(instance = page_roles,many = true) return response(ser.data) # return pg.get_paginated_response(ser.data) |
1
2
3
4
5
6
7
8
9
10
|
# api/utils/serializsers/pager.py from rest_framework import serializers from api import models class pagerserialiser(serializers.modelserializer): class meta: model = models.role fields = "__all__" |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/derek1184405959/p/8727595.html