Serializer - Individual column searching
Serializer - Individual column searching
Hello everyone,
In the serializer. I decrypt the patiendid field(def to_representation), which I saved in the database in encrypted form. I can see the decrypt state in the Patient ID column. No problem so far. However, when I use Individual column searching in the Patient ID column, I cannot find the Patient ID value I want. What am i missing? Thanks in advance.
For example;
Patient ID: 1
The encrypted version is: Z0FBQUFBQmt3U0xVQVdfamlYYlBUZVM5YzUyMUVaTndBM1F3b1Zlc1VJb2ExejViOV91OHjTWFvU1RnZ0hSa2tTWWRJUmZLVTFtRnVjXVX0dxkNDl
When I type 1 in the Individual column searching column, rows with Patient ID 1 do not appear. However:
Z0FBQUFBQmt3U0xVQVdfamlYYlBUZVM5YzUyMUVaTndBM1F3b1Zlc1VJb2ExejViOV91OHhjTWFvU1RnZ0hSa2tTWWRJUmZLVTFtRnVjQUxkNDlvvjQUxkNDlvvXdXVITz
When I type encrypted version, lines with Patient ID 1 appear on the screen.
serializers.py
class SamplesSerializer(serializers.ModelSerializer):
projects = ProjectsSerializer(read_only=True)
hospitals = HospitalsSerializer(read_only=True)
source_samples = SourceSamplesSerializer(read_only=True)
patientid = Epi_dataSerializer(read_only=True)
class Meta:
model = Samples
fields = ['id', 'p_id_s', 'reception_date', 'hospital_date', 'culture', 'index_sample', 'is_index_sample', 'status', 'hospital_sample_number', 'sample_type', 'box', 'last_extraction_date', 'inactivation_date', 'transfer_date', 'comments', 'projects', 'hospitals', 'source_samples', 'patientid']
def to_representation(self, instance):
ret = super().to_representation(instance)
ret['patientid']['patientid'] = decrypt(ret['patientid']['patientid'])
return ret
base.html
"initComplete": function() {
var api = this.api();
$("#records").wrap("<div id='ai' style='overflow:auto; width:100%;'></div>");
api.columns([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94]).eq(0).each(function(colIdx) {
var cell = $('.filters th').eq($(api.column(colIdx).header()).index());
var title = $(cell).text();
$(cell).html('<input type="text" style="width: 90px;" placeholder="' + title + '"/>');
$('input', $('.filters th').eq($(api.column(colIdx).header()).index())).off('keyup change').on('keyup change', function(e) {
e.stopPropagation();
$(this).attr('title', $(this).val());
var regexr = '(^{search}$)';
var curserPosition = this.selectionStart;
api.column(colIdx).search(
this.value != ''? regexr.replace('{search}', '(((' + this.value + ')))')
:'',
this.value != '',
this.value == ''
).draw();
$(this).focus()[0].setSelectionRange(cursorPositon, cursorPositon);
});
});
},
Answers
Its hard to say without seeing the actual issue and table data. Start by posting your Datatables init code and (assuming you are using
ajax
) provide an example of the JSON response using the browser's network inspector. Better is a link to your page or a test case showing the issue so we can help debug.https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Thank you Kevin,
Since I am working on local host, it is not possible for me to share. I don't know if I can adapt it for test case because the code I wrote with Django is quite large.
To summarize briefly, yes, I am using ajax, plus individual column search works for all other columns but not for the Patient ID column. I think it is because, I am decrypting the Patient ID (json below. patientid:6207103) value in the to_represantation function. I did this because I want to see the Patient ID rows that are encrypted in the database, on the datatable without encryption.
Found the row
Nothing found
The JSON response has
recordsTotal
andrecordsFiltered
which leads me to believe that you have server side processing enabled, ie,serverSide: true
. With server side processing all searching, sorting and paging functions are provided by the server script. So the column search is sending an Ajax request to your server script to get the results. See the Server side processing protocol docs for details and use the network inspector to see the request. Your server script will need to encrypt the6207103
search string to search the DB.Kevin