0

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.

IordanouGiannis
  • 4,149
  • 16
  • 65
  • 99

2 Answers2

1

It's should be:

"url": "/my-products/?draw=0&start=0&length=0"

in Ajax and :

draw = int(self.request.GET.get("draw", 0))
start = int(self.request.GET.get("start", 0))
length = int(self.request.GET.get("length", 0))

in DRF

Ykh
  • 7,567
  • 1
  • 22
  • 31
1

The KeyError in python is pretty straightforward. In your case , self.request.GET["draw"] statement tries to return the value draw but draw is not found in the set of existing keys.

KeyError

Raised when a mapping (dictionary) key is not found in the set of existing keys.

The request.GET is a dict like object, So you can use .get() to access values.

The syntax of get() is get(key,[default]). So, it should be as

draw = int(self.request.GET.get("draw", default_val))
start = int(self.request.GET.get("start", default_val))
length = int(self.request.GET.get("length", default_val))

Note: If you doesn't provide default_val, .get() will return None if the Key doesn't exist

You can read more about KeyError in this SO post

JPG
  • 82,442
  • 19
  • 127
  • 206