2

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?

1 Answer 1

4

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)
    )
Sign up to request clarification or add additional context in comments.

1 Comment

Of course! Thank you so much, it now works like a charm. I need to wait 6 hours for accepting your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.