導入測試數據
在Pycharm中連接數據庫
執行sql
測試數據在:https://github.com/zx490336534/ApiTest/tree/master/test
按照順序執行
選擇執行的目標
處理時間格式
從數據庫中可以看出,使用 models.DateTimeField
生成的時間數據格式爲 2019-11-0606:21:19.355242
我們需要返回一個 `2019-11-06 06:21:19
的內容
所以我們需要重寫 list
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
datas = serializer.data
datas = get_count_by_project(datas)
return self.get_paginated_response(datas)
serializer = self.get_serializer(queryset, many=True)
datas = serializer.data
datas = get_count_by_project(datas)
return Response(datas)
將對數據的處理寫在 get_count_by_project
函數中
通過調試可以看到獲取到的datas對象爲一個包含字典的列表
很容易的想到使用 正則
來進行提取,並修改字典內容
mtch = re.search(r'(.*)T(.*)\..*?', item['create_time'])
item['create_time'] = mtch.group(1) + ' ' + mtch.group(2)
獲取項目數據
獲取接口總數
project_id = item['id']
interfaces_testcases_objs = Interfaces.objects.values('id').annotate(testcases=Count('testcases')). \
filter(project_id=project_id, is_delete=False)
interfaces_count = interfaces_testcases_objs.count()
interfaces_testcases_objs
相當於調用了以下SQL語句
SELECT `tb_interfaces`.`id`, COUNT(`tb_testcases`.`id`) AS `testcases` FROM `tb_interfaces` LEFT OUTER JOIN `tb_testcases` ON (`tb_interfaces`.`id` = `tb_testcases`.`interface_id`) WHERE (`tb_interfaces`.`is_delete` = False AND `tb_interfaces`.`project_id` = 1) GROUP BY `tb_interfaces`.`id` ORDER BY NULL
獲取用例總數
testcases_count = 0
for one_dict in interfaces_testcases_objs:
testcases_count += one_dict['testcases']
獲取配置數量
interfaces_configures_objs = Interfaces.objects.values('id').annotate(configures=Count('configures')). \
filter(project_id=project_id, is_delete=False)
configures_count = 0
for one_dict in interfaces_configures_objs:
configures_count += one_dict['configures']
相當於執行了以下SQL
SELECT `tb_interfaces`.`id`, COUNT(`tb_configures`.`id`) AS `configures` FROM `tb_interfaces` LEFT OUTER JOIN `tb_configures` ON (`tb_interfaces`.`id` = `tb_configures`.`interface_id`) WHERE (`tb_interfaces`.`is_delete` = False AND `tb_interfaces`.`project_id` = 1) GROUP BY `tb_interfaces`.`id` ORDER BY NULL
獲取測試套件總數
testsuits_count = Testsuits.objects.filter(project_id=project_id, is_delete=False).count()
整個函數
import re
from django.db.models import Count
from interfaces.models import Interfaces
from testsuits.models import Testsuits
def get_count_by_project(datas):
datas_list = []
for item in datas:
mtch = re.search(r'(.*)T(.*)\..*?', item['create_time'])
item['create_time'] = mtch.group(1) + ' ' + mtch.group(2)
project_id = item['id']
interfaces_testcases_objs = Interfaces.objects.values('id').annotate(testcases=Count('testcases')). \
filter(project_id=project_id, is_delete=False)
interfaces_count = interfaces_testcases_objs.count()
testcases_count = 0
for one_dict in interfaces_testcases_objs:
testcases_count += one_dict['testcases']
interfaces_configures_objs = Interfaces.objects.values('id').annotate(configures=Count('configures')). \
filter(project_id=project_id, is_delete=False)
configures_count = 0
for one_dict in interfaces_configures_objs:
configures_count += one_dict['configures']
testsuits_count = Testsuits.objects.filter(project_id=project_id, is_delete=False).count()
item['interfaces'] = interfaces_count
item['testsuits'] = testsuits_count
item['testcases'] = testcases_count
item['configures'] = configures_count
datas_list.append(item)
return datas_list
結果
返回分頁內容
配置 setting.py
REST_FRAMEWORK = {
'PAGE_SIZE': 10,
'DEFAULT_PAGINATION_CLASS': 'utils.pagination.PageNumberPaginationManual',
}
編寫自定義分頁操作
utils.pagination.PageNumberPaginationManual
from django.conf import settings
from rest_framework import pagination
class PageNumberPaginationManual(pagination.PageNumberPagination):
max_page_size = 50
page_size_query_param = 'size'
page_query_description = '第幾頁'
page_size_query_description = '每頁幾條'
def get_paginated_response(self, data):
response = super(PageNumberPaginationManual, self).get_paginated_response(data)
response.data['total_pages'] = self.page.paginator.num_pages
response.data['current_page_num'] = self.page.number
return response