gpt4 book ai didi

python - 如何使用 django rest 框架的数据表服务器端处理

转载 作者:行者123 更新时间:2023-11-28 22:38:33 27 4
gpt4 key购买 nike

我尝试使用 datatables :服务器端处理

我有一个观点:

class DataList(generics.ListAPIView):
queryset = Data.objects.all()
serializer_class = DataSerializer

还有我的 html:

$(document).ready(function() {
$('#example').dataTable( {

"processing": true,
/* serverSide:true */
"deferRender": true,
"iDisplayLength": 25,
"paging": true,
ajax: {
url: 'http://127.0.0.1:8000/api/datas/',
dataSrc: ''
},
columns: [
{ "data": "id"},
{ "data": "name"},
{ "data": "update_time"},
{ "data": "description"},
]
});

});

这很好用。

然后我注释掉 serverSide:true :

$(document).ready(function() {
$('#example').dataTable( {

"processing": true,
"serverSide": true,
"deferRender": true,
"iDisplayLength": 25,
"paging": true,
ajax: {
url: 'http://127.0.0.1:8000/api/datas/',
dataSrc: ''
},
columns: [
{ "data": "id"},
{ "data": "name"},
{ "data": "update_time"},
{ "data": "description"},
]
});

});

paginatorsearch 以及 ordering 不再工作
好像查询了所有数据。

我看到数据表示例是用 php 编写的。
我想将 datatable 与 django rest framework 集成,我还应该设置什么??

最佳答案

首先,阅读一些关于服务器端数据表处理的文档:https://datatables.net/manual/server-side .它会告诉您什么数据表发送到后端以及它想要返回什么。只需提供所需的参数即可。

然后让我向您展示一些我作品中的代码。这不是完整的示例,但您会知道将什么传递回 datatable() 函数。

一些常量:

import json


DEFAULT_START_POSITION = 0
DEFAULT_PAGE_SIZE = 10
DEFAULT_SORTING_COLUMN_INDEX = 1
DEFAULT_SORTING_METHOD = 'asc'

#constants for querying
ORDER_DICT = {
0: 'field1',
1: 'field2',
}

ORDER_BY = {
'desc': '-',
'asc': ''
}

QUERY_FIELDS = ['field1', 'field2']

Python代码主要部分:

def get_sorting(self, request_values):
return int(request_values.get('iSortCol_0', self.DEFAULT_SORTING_COLUMN_INDEX)),\
request_values.get('sSortDir_0', self.DEFAULT_SORTING_METHOD).lower()

def get_paging(self, request_values):
return int(request_values.get('iDisplayStart', self.DEFAULT_START_POSITION)),\
int(request_values.get('iDisplayLength', self.DEFAULT_PAGE_SIZE))

def render_records(self, current_page_members, query):
# list to be returned for aaData
records = []
# ... do something to put data in the list and return
return records

def get(self, request):
# ... receive the request
# paging
start, page_size = self.get_paging(request_values)
# sorting
sort_column, sort_by = self.get_sorting(request_values)

# research query
query = request_values.get('sSearch', '') # get search term from datatable request
query_combined = self.get_combined_query(query)

all_records, filter_count, current_page_records = \
self.get_member_list(request_values, company_id, query_combined, sort_by, sort_column, start, page_size)

# json dict to be returned
records = {
'iTotalRecords': all_records.count(), # The total count of records in your database, if query exist, it is the count of filtered records
'sEcho': request_values.get('sEcho', '0'), # Represents the rendering count of datatable
'aaData': [], # Main data to be rendered in table
}

# if no query, all records returned
records.update({'iTotalDisplayRecords': filter_count if query else records['iTotalRecords']})

# update aaData list for records
records['aaData'].extend(self.render_records(current_page_records, query))

return HttpResponse(json.dumps(records), content_type='application/json')

请再次注意,这不是一个完整的示例,您应该将其扩展到您自己的需求。

关于python - 如何使用 django rest 框架的数据表服务器端处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35519796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com