Django-filter: 誰かがDjango / Postgresネイティブ全文検索エンジンで使用する例を挙げてもらえますか?

作成日 2019年02月16日  ·  4コメント  ·  ソース: carltongibson/django-filter

ここでは完全に初心者です。

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

Documentation

最も参考になるコメント

こんにちは@gotexis。 django-filterは基本的に2つのことを行います:

  • 入力を検証する
  • それらの入力からクエリを作成します

探しているクエリを構成するFilterクラスがない場合は、次の2つのオプションがあります。

  • 新しいフィルタークラスを作成する
  • Filter.method機能を使用します。

クエリを一般化できる場合は、カスタムFilterを作成する方が再利用しやすくなりますが、 Filter.methodは1回限りのインスタンスに役立ちます。 あなたは次のようなものを書くでしょう:

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

全てのコメント4件

こんにちは@gotexis。 django-filterは基本的に2つのことを行います:

  • 入力を検証する
  • それらの入力からクエリを作成します

探しているクエリを構成するFilterクラスがない場合は、次の2つのオプションがあります。

  • 新しいフィルタークラスを作成する
  • Filter.method機能を使用します。

クエリを一般化できる場合は、カスタムFilterを作成する方が再利用しやすくなりますが、 Filter.methodは1回限りのインスタンスに役立ちます。 あなたは次のようなものを書くでしょう:

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

@rpkilbyの助けに感謝します、このコードを消化するために

今、私はdjango-filtersと統合されたgraphene-djangoで頭を叩いています。

これをGraphQLで(理想的には)実装したいと思っています

グラフェン-djangoがdjango-filterをラップする方法に問題がある可能性があることに注意してください。 例:#927を参照

@rpkilbyはいサー
確かにそこにあるように見えますのPython + GraphQLがノードと比較してとなっている、プラス著者がプロジェクトにあきらめているか不評与えられたグラフェンの問題の、あなたは同意しないだろう:)

django-filterにも関連する最新のものの1つは、DjangoFilterConnectionFieldがプリフェッチで機能しないため、大量のクエリが発生することです。 私はそれをデバッグするのに失敗しました、あなたが共有する経験があるかどうか疑問に思います:)

このページは役に立ちましたか?
0 / 5 - 0 評価