django-rest-framework-datatables
django-rest-framework-datatables copied to clipboard
Smart Filtering on the Server Side
Do we have any support for smart filtering on the server-side?
https://datatables.net/forums/discussion/24637/smart-search-whith-serverside-ssp#Comment_68324 https://datatables.net/forums/discussion/8045/smart-filtering-does-not-work-on-server-side-processing
Thank you for this package, it is very useful.
I guess smart filtering is splitting the search string into words and do the search for each word ? No this is not supported at the moment, If you want to help with a pull request, you're welcome.
my ugly solution: use https://github.com/vsemionov/django-rest-fuzzysearch little magic:
# region 'SmartSearch'
class DatatablesFilterBackend2(DatatablesFilterBackend):
def get_q(self, datatables_query):
q = Q()
return q
def filter_queryset(self, request, queryset, view):
queryset = super().filter_queryset(request, queryset, view).distinct()
return queryset
class SmartFuzzySearchMixin:
filter_backends = (RankedFuzzySearchFilter, DatatablesFilterBackend2)
min_rank = 0.1
# search_fields = ('id', ) # use search by id
def __init__(self):
if hasattr(self, 'search_fields'):
self.search_fields = ('id', ) + self.search_fields # noqa
pass
# endregion
.
.
.
class RequestResultViewSet(SmartFuzzySearchMixin, viewsets.ReadOnlyModelViewSet):
search_fields = ('id', 'context') # define fields for smart search
# the you usual code, not rewrite filter_backends !
for speedup use GinIndex for search fields in models and i have fully working django-rest-framework-datatables but with FuzzySearch filter i know, ugly, but work fine