Open gotexis opened 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:
Filter.method
feature.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 :)
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)
Something in the Tips section would be great.
https://django-filter.readthedocs.io/en/stable/guide/tips.html
Cool, will create a PR soon for this. 😄
Totally noob here....
https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/search/#postgresql-fts-search-configuration