carltongibson / django-filter

A generic system for filtering Django QuerySets based on user selections
https://django-filter.readthedocs.io/en/main/
Other
4.46k stars 769 forks source link

Could anyone give an example to use with Django/Postgres native Full Text Search engine? #1039

Open gotexis opened 5 years ago

gotexis commented 5 years ago

Totally noob here....

https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/search/#postgresql-fts-search-configuration

rpkilby commented 5 years ago

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

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))
gotexis commented 5 years ago

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

rpkilby commented 5 years ago

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

gotexis commented 5 years ago

@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 :)

Pradhvan commented 1 year ago

Just stumbled on this issue when searching how to use django-filter with Postgres Full Text search. Since the issue is already tagged Documentation should we add a how to guide on the approach mentioned by @rpkilby in the sub-section of the doc (I can take that part up)

carltongibson commented 1 year ago

Something in the Tips section would be great.

https://django-filter.readthedocs.io/en/stable/guide/tips.html

Pradhvan commented 1 year ago

Cool, will create a PR soon for this. 😄