0

I need to move the request.POST parameters to the request.query_params QueryDict.

Is there an accepted way of doing this?

Background

I am using datatables, with a DRF backend, which is working fine. I am moving the application to integration and ... it stops working. Why? Request URL too big (on the 7000 characters range) - which was not a problem in my dev host ...

So, I am looking for a solution to that problem. The first solution is to use POST instead of GET. That works, but the library integrating DRF with datatables is not processing the form parameters of the POST request. Because of that, filtering, pagination and so on have stopped working.

The easiest thing to solve this would be to put the form parameters into the query parameters, and let the backend process the request as if it was a normal GET request.

This is what I am doing at the moment:

class DataViewSet(viewsets.ModelViewSet):

    queryset = Data.objects.all()
    serializer_class = DataSerializer

    def create(self, request, *args, **kwargs):
        # DataTable uses a lot of parameters which do not fit into a normal URL. To get the data we need to do POST,
        #   so that the parameters are sent in the body
        # We hijack the create method to list the data
        return self.list(request, *args, **kwargs)
blueFast
  • 41,341
  • 63
  • 198
  • 344

1 Answers1

0

I'm not aware of any accepted ways of doing this. But let me offer you an idea. It is probably on the opposite side of what accepted means.

The rest_framework.request.Request.query_params look like this:

@property
def query_params(self):
    return self._request.GET

Im thinking about substituting the self._request.GET with self._request.POST

class DataViewSet(viewsets.ModelViewSet):

    queryset = Data.objects.all()
    serializer_class = DataSerializer

    def create(self, request, *args, **kwargs):
        # DataTable uses a lot of parameters which do not fit into a normal URL. To get the data we need to do POST,
        #   so that the parameters are sent in the body
        # We hijack the create method to list the data
        request._request.GET = request._request.POST
        return self.list(request, *args, **kwargs)

This should work for POST data. Sending files to this endpoint is probably bad idea.

NOTE: This is very fishy and could introduce bugs in the future. Without look into your code i cannot predict the side effects.

Kamil Niski
  • 4,580
  • 1
  • 11
  • 24