脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - django rest framework 数据的查找、过滤、排序的示例

django rest framework 数据的查找、过滤、排序的示例

2021-03-09 00:12linux_player_c(系统&开发) Python

这篇文章主要介绍了 django rest framework 数据的查找、过滤、排序,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

对于管理系统,常常需要展示列表数据,我们对于列表内的数据常常需要查找、过滤、排序等操作,其中查找等操作大部分是在后台进行的。django rest framework可以轻松的实现数据的查找、过滤等操作。接下来我们将以实际的例子进行介绍。

示例代码github地址:https://github.com/jinjidejuren/drf_learn

例如cmdb系统,作为资产管理系统常常需要对数据进行过滤或查找,获取期望的信息。

实现model

1.在这个示例项目中,需要实现对物理服务器的条件过滤,物理服务器的model列表如下(apps/assets/models.py文件):

?
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
class server(models.model):
  """
  物理服务器
  """
  status_choice = (
    ('online', '上线'),
    ('offline', '下线'),
    ('normal', '正常'),
    ('abnormal', '异常')
  )
 
  server_name = models.charfield(verbose_name=u'服务器名称', max_length=128, blank=false, null=false)
  server_num = models.charfield(verbose_name=u'服务器编号', max_length=128, blank=true, null=true)
  brand = models.charfield(verbose_name=u'品牌', max_length=64, blank=true, null=true)
  model = models.charfield(verbose_name=u'型号', max_length=64, blank=true, null=true)
  cpus = models.integerfield(verbose_name=u'cpu核数', default=0)
  ram = models.integerfield(verbose_name=u'内存大小', default=0)
  disk = models.integerfield(verbose_name=u'磁盘大小', default=0)
  product_date = models.datetimefield(verbose_name=u'生产日期', auto_now_add=true)
  status = models.charfield(verbose_name=u'状态', max_length=16, choices=status_choice)
 
  created_time = models.datetimefield(verbose_name=u'创建时间', auto_now_add=true)
  modified_time = models.datetimefield(verbose_name=u'修改时间', auto_now_add=true)
 
  class meta:
    verbose_name = u'服务器'
    verbose_name_plural = verbose_name
 
  def __str__(self):
    return self.server_name

实现serializer

接下来需要实现server这个model的序列化类,在apps/assets/serializers.py中编写:

?
1
2
3
4
5
6
7
8
9
10
class serviceserializer(serializers.modelserializer):
  """
  服务器序列化
  """
 
  class meta:
    model = server
    fields = ('id', 'server_name', 'server_num', 'brand', 'model', 'cpus',
         'ram', 'disk', 'product_date', 'status', 'created_time',
         'modified_time')

对于fields来说,可以使用 _ all _ 来代表所有的字段,除了model中定义的field外,序列化还可以指定其他的信息,比如嵌套信息或者自定义的信息。具体可以取决于业务逻辑。

实现modelviewset

对于modelviewset,我们可以围绕它对用户请求做相应的处理。常见的是对model进行增加、删除、查找、修改等。在这部分我们需要实现serverviewset:

?
1
2
3
4
5
6
7
class serverviewset(viewsets.modelviewset):
  """
  物理服务器视图
  """
  queryset = server.objects.all().order_by('-created_time')
  serializer_class = serverserializer
  pagination_class = myformatresultssetpagination

queryset指定返回列表的形式,所有的信息都返回,并且按照创建时间逆序排列,这样可以把最新的信息先返回,比较符合用户的操作习惯。

serializer_class定义了返回的序列化格式为serverserializer所指定的fields内容

pagination_class 指定了分页的类型,这个myformatresultssetpagination是我们的自定义类型

实现router

如果用户想要访问server的信息,需要指定server的路由,这个和之前介绍的类似。需要的嗯一个一个router对象,并且将server的路由注册进去。

?
1
2
3
4
5
6
7
8
from rest_framework import routers
 
router = routers.defaultrouter()
router.register(r'servers', views.serverviewset, base_name='servers')
 
urlpatterns = [
  url(r'^', include(router.urls))
]

对于servers的访问都由serverviewset进行处理。

尝试访问

http://127.0.0.1:8060/assets/v1/servers/ ,信息如下:

django rest framework 数据的查找、过滤、排序的示例

注:我们需要添加示例信息,作为后续的各种测试使用。

按照条件获取

在日常操作中,我们需要获取指定条件的数据,例如对于物理服务器,我们需要指定品牌、指定cpu核数、指定内存大小等。有时候我们需要按照cpu核数进行排序。这些都需要我们对serverviewset进行更多的拓展。

如果进行条件过滤,需要首先安装django-filter模块:

?
1
pip install django-filter

在配置文件settings/base.py中添加应用django_filters:

?
1
2
3
4
5
6
7
8
9
10
11
12
installed_apps = [
  # 'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'rest_framework',
  'django_filters',
  'apps.assets',
  'apps.rbac'
]

在apps/assets/views.py顶部包含如下包:

?
1
2
3
from django_filters.rest_framework import djangofilterbackend
from rest_framework import filters
from django_filters import rest_framework

serverviewset可以添加相应的过滤条件:

?
1
2
3
4
5
6
7
8
9
10
11
12
class serverviewset(viewsets.modelviewset):
  """
  物理服务器视图
  """
  queryset = server.objects.all()
  serializer_class = serverserializer
  pagination_class = myformatresultssetpagination
  filter_backends = (rest_framework.djangofilterbackend, filters.searchfilter, filters.orderingfilter, )
  filter_class = serverfilter
  search_fields = ('server_name', '=brand', 'status', )
  ordering_fields = ('cpus', 'ram', 'disk', 'product_date', )
  ordering = ('-created_time', )

这里的filter_backends指定了过滤的类型,此处设定了djangofilterbackend(过滤)、searchfilter(搜索)和orderingfilter(排序)。

1.过滤

过滤设定了过滤的配置类为serverfilter,关于serverfilter在apps/assets/filters.py文件中进行了定义:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import django_filters
 
from .models import *
 
 
class serverfilter(django_filters.rest_framework.filterset):
  """
  物理服务器过滤器
  """
 
  server_name = django_filters.charfilter(name='server_name', lookup_expr='icontains')
  brand = django_filters.charfilter(name='brand', lookup_expr='icontains')
  cpus = django_filters.numberfilter(name='cpus')
  ram = django_filters.numberfilter(name='ram')
  disk = django_filters.numberfilter(name='disk')
 
  class meta:
    model = server
    fields = ['server_name', 'brand', 'cpus', 'ram', 'disk', ]

也就是说可以通过'server_name', ‘brand', ‘cpus', ‘ram', ‘disk'对物理服务器的信息进行过滤,得到相应的序列化列表。

例如获取cpu为24核的物理服务器:

django rest framework 数据的查找、过滤、排序的示例

得到物理服务器列表中cpu都为24:

?
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
get /assets/v1/servers/?server_name=&brand=&cpus=24&ram=&disk=
http 200 ok
allow: get, post, head, options
content-type: application/json
vary: accept
 
{
  "results": [
    {
      "id": 9,
      "server_name": "data-server2",
      "server_num": "server-01-shanghai",
      "brand": "hp",
      "model": "hpe apollo 4200 gen9",
      "cpus": 24,
      "ram": 64,
      "disk": 2500,
      "product_date": "2018-06-23t13:51:09.641473z",
      "status": "online",
      "created_time": "2018-06-23t13:51:09.642583z",
      "modified_time": "2018-06-23t13:51:09.642764z"
    },
    {
      "id": 8,
      "server_name": "data-server2",
      "server_num": "server-01-shanghai",
      "brand": "hp",
      "model": "hpe apollo 4200 gen9",
      "cpus": 24,
      "ram": 64,
      "disk": 5000,
      "product_date": "2018-06-23t13:51:02.466031z",
      "status": "online",
      "created_time": "2018-06-23t13:51:02.467274z",
      "modified_time": "2018-06-23t13:51:02.467471z"
    },
    {
      "id": 7,
      "server_name": "data-server1",
      "server_num": "server-01-shanghai",
      "brand": "hp",
      "model": "hpe apollo 4200 gen9",
      "cpus": 24,
      "ram": 64,
      "disk": 5000,
      "product_date": "2018-06-23t13:50:55.622403z",
      "status": "offline",
      "created_time": "2018-06-23t13:50:55.623315z",
      "modified_time": "2018-06-23t13:50:55.623431z"
    },
    {
      "id": 6,
      "server_name": "data-server",
      "server_num": "server-01-shanghai",
      "brand": "hp",
      "model": "hpe apollo 4200 gen9",
      "cpus": 24,
      "ram": 64,
      "disk": 5000,
      "product_date": "2018-06-23t13:50:48.088028z",
      "status": "online",
      "created_time": "2018-06-23t13:50:48.089433z",
      "modified_time": "2018-06-23t13:50:48.089703z"
    },
    {
      "id": 5,
      "server_name": "harbor-server3",
      "server_num": "server-01-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:49:27.590015z",
      "status": "offline",
      "created_time": "2018-06-23t13:49:27.590980z",
      "modified_time": "2018-06-23t13:49:27.591097z"
    },
    {
      "id": 4,
      "server_name": "harbor-server3",
      "server_num": "server-01-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:49:23.783337z",
      "status": "abnormal",
      "created_time": "2018-06-23t13:49:23.784243z",
      "modified_time": "2018-06-23t13:49:23.784500z"
    },
    {
      "id": 3,
      "server_name": "harbor-server2",
      "server_num": "server-01-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:49:16.348672z",
      "status": "online",
      "created_time": "2018-06-23t13:49:16.349555z",
      "modified_time": "2018-06-23t13:49:16.349663z"
    },
    {
      "id": 2,
      "server_name": "harbor-server1",
      "server_num": "server-02-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:48:57.853354z",
      "status": "online",
      "created_time": "2018-06-23t13:48:57.853990z",
      "modified_time": "2018-06-23t13:48:57.854098z"
    },
    {
      "id": 1,
      "server_name": "harbor-server",
      "server_num": "server-01-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:48:48.777153z",
      "status": "online",
      "created_time": "2018-06-23t13:48:48.778048z",
      "modified_time": "2018-06-23t13:48:48.778166z"
    }
  ],
  "pagination": 9,
  "page_size": 10,
  "page": 1
}

2.搜索

搜索需要指定 search 关键字需要查询的信息,例如搜索名称为‘test'开头的服务器:

http://127.0.0.1:8060/assets/v1/servers/?search=test

获取列表:

?
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
http 200 ok
allow: get, post, head, options
content-type: application/json
vary: accept
 
{
  "results": [
    {
      "id": 14,
      "server_name": "test-server1",
      "server_num": "server-01-shanghai",
      "brand": "dell",
      "model": "modular",
      "cpus": 32,
      "ram": 256,
      "disk": 500,
      "product_date": "2018-06-23t13:52:40.583743z",
      "status": "offline",
      "created_time": "2018-06-23t13:52:40.584409z",
      "modified_time": "2018-06-23t13:52:40.584512z"
    },
    {
      "id": 13,
      "server_name": "test-server",
      "server_num": "server-01-shanghai",
      "brand": "dell",
      "model": "modular",
      "cpus": 32,
      "ram": 256,
      "disk": 2500,
      "product_date": "2018-06-23t13:52:24.760819z",
      "status": "normal",
      "created_time": "2018-06-23t13:52:24.761475z",
      "modified_time": "2018-06-23t13:52:24.761578z"
    }
  ],
  "pagination": 2,
  "page_size": 10,
  "page": 1
}

在search_fields中可以指定多种查找方式:

‘^name' 以name开头

‘=name' 精确匹配

全局检索(只有mysql数据源支持)

‘$' 正则匹配

对应的search_fileds示例如下:

?
1
search_fields = ('^server_name', '=brand', 'status', )

3.排序

在ordering字段指定了默认排序方式(按照创建时间逆序排序):

?
1
ordering = ('-created_time', )

也可以使用如下方式指定:

?
1
queryset = server.objects.all().order_by('-created_time')

如果要自定义排序字段,需要指定 ordering 字段的内容:

例如按照内存大小排列服务器:

http://127.0.0.1:8060/assets/v1/servers/?ordering=ram

获取的信息列表如下:

?
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
http 200 ok
allow: get, post, head, options
content-type: application/json
vary: accept
 
{
  "results": [
    {
      "id": 6,
      "server_name": "data-server",
      "server_num": "server-01-shanghai",
      "brand": "hp",
      "model": "hpe apollo 4200 gen9",
      "cpus": 24,
      "ram": 64,
      "disk": 5000,
      "product_date": "2018-06-23t13:50:48.088028z",
      "status": "online",
      "created_time": "2018-06-23t13:50:48.089433z",
      "modified_time": "2018-06-23t13:50:48.089703z"
    },
    {
      "id": 7,
      "server_name": "data-server1",
      "server_num": "server-01-shanghai",
      "brand": "hp",
      "model": "hpe apollo 4200 gen9",
      "cpus": 24,
      "ram": 64,
      "disk": 5000,
      "product_date": "2018-06-23t13:50:55.622403z",
      "status": "offline",
      "created_time": "2018-06-23t13:50:55.623315z",
      "modified_time": "2018-06-23t13:50:55.623431z"
    },
    {
      "id": 8,
      "server_name": "data-server2",
      "server_num": "server-01-shanghai",
      "brand": "hp",
      "model": "hpe apollo 4200 gen9",
      "cpus": 24,
      "ram": 64,
      "disk": 5000,
      "product_date": "2018-06-23t13:51:02.466031z",
      "status": "online",
      "created_time": "2018-06-23t13:51:02.467274z",
      "modified_time": "2018-06-23t13:51:02.467471z"
    },
    {
      "id": 9,
      "server_name": "data-server2",
      "server_num": "server-01-shanghai",
      "brand": "hp",
      "model": "hpe apollo 4200 gen9",
      "cpus": 24,
      "ram": 64,
      "disk": 2500,
      "product_date": "2018-06-23t13:51:09.641473z",
      "status": "online",
      "created_time": "2018-06-23t13:51:09.642583z",
      "modified_time": "2018-06-23t13:51:09.642764z"
    },
    {
      "id": 1,
      "server_name": "harbor-server",
      "server_num": "server-01-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:48:48.777153z",
      "status": "online",
      "created_time": "2018-06-23t13:48:48.778048z",
      "modified_time": "2018-06-23t13:48:48.778166z"
    },
    {
      "id": 2,
      "server_name": "harbor-server1",
      "server_num": "server-02-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:48:57.853354z",
      "status": "online",
      "created_time": "2018-06-23t13:48:57.853990z",
      "modified_time": "2018-06-23t13:48:57.854098z"
    },
    {
      "id": 3,
      "server_name": "harbor-server2",
      "server_num": "server-01-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:49:16.348672z",
      "status": "online",
      "created_time": "2018-06-23t13:49:16.349555z",
      "modified_time": "2018-06-23t13:49:16.349663z"
    },
    {
      "id": 4,
      "server_name": "harbor-server3",
      "server_num": "server-01-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:49:23.783337z",
      "status": "abnormal",
      "created_time": "2018-06-23t13:49:23.784243z",
      "modified_time": "2018-06-23t13:49:23.784500z"
    },
    {
      "id": 5,
      "server_name": "harbor-server3",
      "server_num": "server-01-beijing",
      "brand": "dell",
      "model": "rack",
      "cpus": 24,
      "ram": 128,
      "disk": 5000,
      "product_date": "2018-06-23t13:49:27.590015z",
      "status": "offline",
      "created_time": "2018-06-23t13:49:27.590980z",
      "modified_time": "2018-06-23t13:49:27.591097z"
    },
    {
      "id": 10,
      "server_name": "data-server2",
      "server_num": "server-01-shanghai",
      "brand": "hp",
      "model": "hpe apollo 4200 gen9",
      "cpus": 32,
      "ram": 256,
      "disk": 2500,
      "product_date": "2018-06-23t13:51:30.706187z",
      "status": "online",
      "created_time": "2018-06-23t13:51:30.707754z",
      "modified_time": "2018-06-23t13:51:30.707878z"
    }
  ],
  "pagination": 14,
  "page_size": 10,
  "page": 1
}

上述的排序、过滤等操作可以组合使用,一般为前端的列表搜索查询提供接口支持。

小结

本章小结的内容介绍了django rest framework如何进行model的定义、序列化、增删改查以及搜索、排序等功能,是书写后端接口必须掌握的技巧。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/linux_player_c/article/details/80779059

延伸 · 阅读

精彩推荐