Search code examples
djangodjango-filter

Django-filter, multiple fields search overriding other parameters! (django-filter)


I am having a spot of trouble making a search box that looks through both the title, and the description of the model, without it over riding everything. I took my example from this: Django-filter, how to make multiple fields search? (with django-filter!)

so my current filter looks something like:

class QuestionSetFilter(django_filters.FilterSet):
    search = CharFilter(method='my_custom_filter', label='search')    

    o = OrderingFilter(
        fields = (
            ('date_posted', 'date_posted')
        ),
        choices = [
            ('date_posted', 'Old first'),
            ('-date_posted', 'New first'),
        ]     
    )
    
    class Meta:
        model = QuestionSet
        exclude = ['image', 'user', 'date_posted', 'question_set_description', 'question_set_title']

    def my_custom_filter(self, queryset, name, value):
        return QuestionSet.objects.filter(
            Q(question_set_title__icontains=value) | Q(question_set_description__icontains=value)
        )    

How can I change my custom filter to still chain with the other parameters I want to search with?


Solution

  • Simple enough, just had to use

    queryset.filter(..)

    instead of

    QuestionSet.objects.filter(..)