I am trying to use DRF and Datatables to populate a table with a large amount of data, with server-side processing.
Below is my view:
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)
This the script for the Datatables and 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": [....],
});
});
I get the following error: KeyError: 'draw'
Probably it means that I don't use the proper way to get the parameter. Although, that's my first attempt at DRF and Datatables with server-side processing, so it's possible there are other mistakes too.