Datatables sending ajax parameter for filtering and search in very complicated way. Which we need to mapping and parse based on each column.
Some example request :
1 | http://localhost:8000/api/somemodel/?format=json&sEcho=4&iColumns=7&sColumns=%2C%2C%2C%2C%2C%2C&iDisplayStart=0&iDisplayLength=10&mDataProp_0=&sSearch_0=&bRegex_0=false&bSearchable_0=true&mDataProp_1=cif&sSearch_1=22222&bRegex_1=false&bSearchable_1=true&mDataProp_2=name&sSearch_2=&bRegex_2=false&bSearchable_2=true&mDataProp_3=gender&sSearch_3=&bRegex_3=false&bSearchable_3=true&mDataProp_4=ktp&sSearch_4=&bRegex_4=false&bSearchable_4=true&mDataProp_5=location&sSearch_5=&bRegex_5=false&bSearchable_5=true&mDataProp_6=&sSearch_6=&bRegex_6=false&bSearchable_6=true&sSearch=&bRegex=false |
I have no idea about where the filtering and search query come from in this request. Therefore, we need to auto-mapping it by :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import re from urlparse import parse_qs """Filtering get params from datatables and mapping into django filter""" def filter_request(self): # Filtering input input_request = parse_qs(self.request.GET.urlencode()) query_dict = {} for key, value in input_request.iteritems(): if(re.search(‘sSearch_’, key)): if value: key_index = input_request.get(‘mDataProp_%s’ % key.split(‘_’)[1]) if key_index is not None: query_dict[key_index[0]] = value[0] return query_dict |
And we get nice results :
1 | {u’some_field’: u’some_value’, …} |