I have this filter:
class MsTuneFilter(django_filters.FilterSet):
def all_titles(self, queryset, name, value):
return MsTune.objects.filter(
Q(name__icontains=value) | Q(title__icontains=value) | Q(alt_title__icontains=value)
)
def ind_1(self, queryset, name, value):
return MsTune.objects.filter(
Q(index_standard_1__startswith=value) | Q(index_gore_1__startswith=value) | Q(alt_index_gore_1__startswith=value) | Q(alt_index_standard_1__startswith=value)
)
title = django_filters.CharFilter(method='all_titles', label="All title fields")
index_standard_1 = django_filters.CharFilter(method='ind_1', label="Index 1")
class Meta:
model = MsTune
fields = ['index_standard_1', 'title', ....]
It all works well when I'm making queries which do not involve both 'title' and 'index_standard_1'. Nevertheless, if I'm searching for something with a specific title AND with a specific index, either the index search or the title is ignored, i.e. the query returns all the indexes or all the titles, ignoring a parameter of my search. What am I overlooking?
You need to filter the queryset
, not make two querysets from the model, so:
def all_titles(self, queryset, name, value):
return queryset.filter(
Q(name__icontains=value) | Q(title__icontains=value) | Q(alt_title__icontains=value)
)
def ind_1(self, queryset, name, value):
return queryset.filter(
Q(index_standard_1__startswith=value) | Q(index_gore_1__startswith=value) | Q(alt_index_gore_1__startswith=value) | Q(alt_index_standard_1__startswith=value)
)