Datatables / Django search behavior

Datatables / Django search behavior

MrKementariMrKementari Posts: 8Questions: 3Answers: 0

Hello,
I'm using DatatablesJS in serverSide processing mode with Django and the djangorestframework-datatables plugin, and I'm a bit lost.

Currently, when I enter 2 search criteria in the search bar, the table only displays the records containing only these 2 criteria in a row, for example: if I enter “hello mister”, the table will only display the records containing these criteria in a row, but not those containing “hello dear mister”. Basically, I'd like the records retrieved to be those containing these 2 search criteria, whatever their position in the record. I think this is the “smart” mode when you're in clientSide processing mode. I hope I've made myself clear :-)

I don't know where to go to modify my code to have this function. If anyone can enlighten me on the subject ...

Here is parts of the code I'm using.
(views.py)

class EvenementAdmViewSet(viewsets.ModelViewSet):
    queryset = EvenementAdm.objects.values('num_equipe_adm','date','heure','executant','hostname','description','action_effectuee')
    serializer_class = EvenementAdmSerializer

(urls.py)

router = routers.DefaultRouter()
router.register(r'EvenementAdm', views.EvenementAdmViewSet)

urlpatterns = [
    ...
    path("add_evenement", views.add_evenement, name="add_evenement"),
    path("api/", include(router.urls))
]

(serializers.py)

class EvenementAdmSerializer(serializers.ModelSerializer):
    class Meta:
        model = EvenementAdm
        fields = (
            'num_equipe_adm','date','heure','executant','hostname','description','action_effectuee'
        )

(settings.py)

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
        'rest_framework_datatables.renderers.DatatablesRenderer',
    ),
    'DEFAULT_FILTER_BACKENDS': (
        'rest_framework_datatables.filters.DatatablesFilterBackend',
    ),
    'DEFAULT_PAGINATION_CLASS': 'rest_framework_datatables.pagination.DatatablesPageNumberPagination',
    'PAGE_SIZE': 50,
}

(mydatatables.js)

// Call the dataTables jQuery plugin to create dataTable for Main Courante
$(document).ready(function() {
    $('#dataTableMainCourante')
        .on('init.dt', function() {
            document.getElementById('divMainCourante').removeAttribute('hidden');
            document.getElementById('divChargement').setAttribute('hidden', '');
        })
        .DataTable({
            serverSide: true,
            ajax: {
                url: '/tools/api/EvenementAdm/?format=datatables',
            },
            columns: [
                { data: 'num_equipe_adm', searchable: false, visible: false },
                { data: 'date', searchable: false, width: "8%" },
                { data: 'heure', searchable: false, orderable: false },
                { data: 'executant', orderable: false },
                { data: 'hostname', orderable: false },
                {
                    data: 'description',
                    orderable: false,
                    render: function(data, type, row, meta) {
                        ...
                    }
                },
                {
                    data: 'action_effectuee',
                    orderable: false,
                    render: function(data, type, row, meta) {
                        ...
                        }
                        else
                            return data;
                    }
                },
                {
                    data: 'num_equipe_adm',
                    type: 'html',
                    orderable: false,
                    searchable: false,
                    render: function(data, type) {
                        ...
                    }
                }
            ],
            order: [[1, 'desc']]
        });
});

Answers

  • kthorngrenkthorngren Posts: 21,862Questions: 26Answers: 5,051

    The djangorestframework-datatables is developed by a third party. This library will need to be modified, by you or someone else, to support the search functionality you want. I suspect the filters.py file is where the modification would need to take place. You may want to look at this question about adding smart search posted for the developer. Sounds like the developer will welcome a PR with the enhancement.

    Kevin

Sign In or Register to comment.