我正在尝试使用DRF和Datatables来填充一个包含大量数据的表,并进行服务器端处理。
下面是我的观点:
class ProductsListAPIView(LoginRequiredMixin, ListAPIView):
authentication_classes = (authentication.SessionAuthentication,)
permission_classes = (permissions.IsAuthenticated,)
serializer_class = ProductSerializer
def get_queryset(self):
qs = Product.objects.filter(user=self.request.user).order_by("-timestamp")
return qs
def list(self, request, *args, **kwargs):
draw = int(self.request.GET["draw"])
start = int(self.request.GET["start"])
length = int(self.request.GET["length"])
queryset = self.get_queryset()
queryset = queryset[start:start+length]
serializer = ProductSerializer(queryset, many=True)
result = {"draw": draw,
"recordsTotal": queryset.count(),
"recordsFiltered":queryset.count(),
"data": serializer.data}
return Response(result)
下面是Datatables和Ajax的脚本:
$(document).ready(function() {
$('#exampleAjax2').dataTable( {
"autoWidth": true,
"displayLength": 10,
"lengthChange": false,
"ordering": false,
"processing": true,
"searching": false,
"serverSide": true,
"language": {
"zeroRecords": "Nothing to display",
"info": "Showing _START_ to _END_ of _TOTAL_ records",
"infoEmpty": ""
},
"ajax": {
"processing": true,
"url": "/my-products/",
"dataSrc": ""
},
"columns": [....],
});
});
我得到以下错误:KeyError: 'draw'
这可能意味着我没有使用正确的方法来获取参数。虽然这是我第一次尝试使用DRF和Datatables进行服务器端处理,但也可能存在其他错误。
发布于 2018-07-13 09:31:25
它应该是:
"url": "/my-products/?draw=0&start=0&length=0"
在Ajax和:
draw = int(self.request.GET.get("draw", 0))
start = int(self.request.GET.get("start", 0))
length = int(self.request.GET.get("length", 0))
在DRF中
https://stackoverflow.com/questions/51316201
复制相似问题