Django-filter: Could anyone give an example to use with Django/Postgres native Full Text Search engine?

Created on 16 Feb 2019  ·  4Comments  ·  Source: carltongibson/django-filter

Documentation

Most helpful comment

Hi @gotexis. django-filter basically does two things:

  • Validate inputs
  • Construct queries from those inputs

When there isn't a Filter class that constructs the query you're looking for, you have two options:

If you can generalize the query, writing a custom Filter would be more reusable, however Filter.method is useful for one-off instances. You would write something like:

from django.contrib.postgres.search import SearchQuery, SearchVector
from djanog_filters import FilterSet, CharFilter

class F(FilterSet):
    # The model `field_name` is the field you want to search on and is passed to your `method`.
    # The `method` argument is the name of the method to call to perform filtering.
    search = CharFilter(field_name='body_text', method='search_fulltext')

    def search_fulltext(self, queryset, field_name, value):
        return queryset \
            .annotate(search=SearchVector(field_name)) \
            .filter(search=SearchQuery(value))

All 4 comments

Hi @gotexis. django-filter basically does two things:

  • Validate inputs
  • Construct queries from those inputs

When there isn't a Filter class that constructs the query you're looking for, you have two options:

If you can generalize the query, writing a custom Filter would be more reusable, however Filter.method is useful for one-off instances. You would write something like:

from django.contrib.postgres.search import SearchQuery, SearchVector
from djanog_filters import FilterSet, CharFilter

class F(FilterSet):
    # The model `field_name` is the field you want to search on and is passed to your `method`.
    # The `method` argument is the name of the method to call to perform filtering.
    search = CharFilter(field_name='body_text', method='search_fulltext')

    def search_fulltext(self, queryset, field_name, value):
        return queryset \
            .annotate(search=SearchVector(field_name)) \
            .filter(search=SearchQuery(value))

Wow thanks for your help @rpkilby, please give me some more days to digest this code...

Right now I am banging my heads with graphene-django which integrates with django-filters.

I hope to (ideally) implement this with GraphQL

Note that there may be some issues with how graphene-django wraps django-filter. e.g.,see #927

@rpkilby Yes sir
It looks like there is indeed heaps of issues with Graphene given how unpopular Python+GraphQL has become as compared to Node, plus the author is giving up on the project, wouldn't you agree :)

One of the most recent one, also related to django-filter, is their DjangoFilterConnectionField doesn't work with prefetching, resulting in tons of queries. I have been failing to debug it, I wonder if you have any experience to share :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xtrinch picture xtrinch  ·  4Comments

jwineinger picture jwineinger  ·  3Comments

techdragon picture techdragon  ·  5Comments

Sonictherocketman picture Sonictherocketman  ·  3Comments

sassanh picture sassanh  ·  4Comments