You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Our use-case for this is to get filter data from request.data instead of request.query_params so that we can use django-filter for POST requests too. The reason why we need this is that we have a couple of APIs where the URL for the GET requests may (in extreme cases) exceed the max length for a URL in some browsers (around 2000 characters).
We have solved this with a custom subclass of DjangoFilterBackend like this:
classCustomDjangoFilterBackend(django_filters.rest_framework.DjangoFilterBackend):
""" Custom DjangoFilterBackend that makes it easy to override the kwargs for the ``filter_class``. """defget_filter_class_data(self, request):
""" Get the ``data`` argument for the ``filter_class`` constructor. Used by :meth:`.get_filter_class_kwargs`. Args: request (django.http.HttpRequest): Django HTTP request object. Returns: dict: A dict-like object, such as a QueryDict or just a dict. """returnrequest.query_paramsdefget_filter_class_kwargs(self, request, queryset):
""" Get kwargs for the ``filter_class`` constructor. Args: request (django.http.HttpRequest): Django HTTP request object. Returns: dict: The kwargs for ``filter_class``. """return {
'queryset': queryset,
'request': request,
'data': self.get_filter_class_data(request=request)
}
deffilter_queryset(self, request, queryset, view):
filter_class=self.get_filter_class(view, queryset)
iffilter_class:
filterset=filter_class(**self.get_filter_class_kwargs(request=request, queryset=queryset))
returnfilterset.qsreturnqueryset
And a subclass of that class for the APIs that allow filters to be received as POST data:
classCustomDjangoFilterBackendPostData(CustomDjangoFilterBackend):
""" Works just like ``CustomDjangoFilterBackend`` for GET request, but gets filter data from ``request.data`` in ``POST`` requests. We use this in the (very few) APIs where the querystring may become larger than the max length for some browsers (~2000 chars). """defget_filter_class_data(self, request):
""" Add support for getting data from ``request.data`` for ``POST`` requests. """ifrequest.method=='POST':
returnrequest.datareturnsuper().get_filter_class_data(request=request)
I think this would be something to consider including in the django-filter codebase. I can see other use-cases too, such as forwarning arguments from request.session or other middleware data into the filters.
The text was updated successfully, but these errors were encountered:
Our use-case for this is to get filter data from
request.data
instead ofrequest.query_params
so that we can use django-filter for POST requests too. The reason why we need this is that we have a couple of APIs where the URL for the GET requests may (in extreme cases) exceed the max length for a URL in some browsers (around 2000 characters).We have solved this with a custom subclass of DjangoFilterBackend like this:
And a subclass of that class for the APIs that allow filters to be received as POST data:
I think this would be something to consider including in the django-filter codebase. I can see other use-cases too, such as forwarning arguments from request.session or other middleware data into the filters.
The text was updated successfully, but these errors were encountered: