Skip to content

Commit a4747ec

Browse files
added global search support to YADCFModelMultipleChoiceFilter
1 parent 979370c commit a4747ec

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

example/albums/templates/albums/albums.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ <h4 class="bg-primary text-white p-2" style="margin: 15px;">Example using column
150150
},
151151
"columns": [
152152
{"data": "rank", "searchable": false},
153-
{"data": "artist_name", "name": "artist_name",},
154-
{"data": "name"},
153+
{"data": "artist_name", "name": "artist_name"},
154+
{"data": "name", "searchable": false},
155155
]
156156
});
157157
yadcf.init(albumFilterTable, [

example/albums/views.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.db.models import Q
12
from django.shortcuts import render
23
from django_filters import widgets, fields, filters, NumberFilter, CharFilter
34

@@ -7,6 +8,7 @@
78
from rest_framework.views import APIView
89

910
from rest_framework_datatables import pagination as dt_pagination
11+
from rest_framework_datatables.django_filters.filters import GlobalFilter
1012
from rest_framework_datatables.django_filters.filterset import DatatablesFilterSet
1113
from rest_framework_datatables.django_filters.backends import DatatablesFilterBackend
1214

@@ -77,23 +79,41 @@ class YADCFModelMultipleChoiceField(fields.ModelMultipleChoiceField):
7779
class YADCFModelMultipleChoiceFilter(filters.ModelMultipleChoiceFilter):
7880
field_class = YADCFModelMultipleChoiceField
7981

82+
def global_q(self):
83+
"""
84+
This method is necessary for the global filter
85+
- i.e. any string values entered into the search box.
86+
"""
87+
if not self._global_search_value:
88+
return Q()
89+
kw = "{}__{}".format(self.field_name, self.lookup_expr)
90+
return Q(**{kw: self._global_search_value})
91+
92+
93+
class GlobalCharFilter(GlobalFilter, filters.CharFilter):
94+
pass
95+
96+
97+
class GlobalNumberFilter(GlobalFilter, filters.NumberFilter):
98+
pass
99+
80100

81101
class AlbumFilter(DatatablesFilterSet):
82102

83-
# the name of this attribute must match the declared 'name' attribute in
103+
# the name of this attribute must match the declared 'data' attribute in
84104
# the DataTables column
85105
artist_name = YADCFModelMultipleChoiceFilter(
86-
field_name="artist", queryset=Artist.objects.all()
106+
field_name="artist__name", queryset=Artist.objects.all(), lookup_expr="contains"
87107
)
88108

89109
# additional attributes need to be declared so that sorting works
90110
# the field names must match those declared in the DataTables columns.
91-
rank = NumberFilter()
92-
name = CharFilter()
111+
rank = GlobalNumberFilter()
112+
name = GlobalCharFilter()
93113

94114
class Meta:
95115
model = Album
96-
fields = ("artist",)
116+
fields = ("artist_name", )
97117

98118

99119
class AlbumFilterListView(generics.ListAPIView):

rest_framework_datatables/django_filters/filters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class GlobalFilter(SwitchRegexFilter):
5252
global filtering without using django-filter.
5353
5454
*Must* be used with DjangoFilterBackend which combines the global
55-
and colum searches in the correct way and with
55+
and column searches in the correct way and with
5656
DatatablesFilterSet, which ensures that the correct attributes are
5757
set to make this work.
5858
@@ -81,7 +81,7 @@ class GlobalFilter(SwitchRegexFilter):
8181
"""
8282

8383
def global_q(self):
84-
"""Return a Q-Object for the local search for this column"""
84+
"""Return a Q-Object for the global search for this column"""
8585
ret = Q()
8686
if self.global_search_value:
8787
ret = Q(**{self.global_lookup: self.global_search_value})

0 commit comments

Comments
 (0)