0

私は現在、elasticsearchバックエンドを備えたdjango-haystack検索エンジンとdrf-haystackを使用してビューを証明しています。現在のユーザーによるdrf-haystack結果のフィルタリング

一般的な検索は素晴らしいですが、私は現在のユーザーによって結果をフィルタリングすることができません。ビューのための

class SectionIndex(indexes.SearchIndex, indexes.Indexable): 
    text = indexes.EdgeNgramField(document=True, use_template=True, template_name="indexes/structure_text.txt") 
    pkey = indexes.IntegerField(model_attr='pk') 
    title = indexes.CharField() 

は(もHaystackSerializerが含まれていますが、そのジェネリックを含む価値がない):

インデックスです。

class SectionSearchView(HaystackViewSet): 
    index_models = [Section] 
    serializer_class = SectionViewSerializer 
    pagination_class = None 
    filter_backend = SectionFilter 

    def get(self, request, *args, **kwargs): 
     return self.list(request, *args, **kwargs) 

    def filter_queryset(self, queryset): 
     queryset = super(HaystackGenericAPIView, self).filter_queryset(queryset) 
     return queryset.using('section') 

そして最後に、フィルタ:

class SectionFilter(HaystackFilter): 
    mine = django_filters.MethodFilter(action='get_mine') 

    class Meta: 
     model = Section 
     fields = ['mine'] 

    def get_mine(self, queryset, value): 
     try: 
      teacher = self.request.user.teacherprofile 
      return queryset.filter(supervisors=teacher) 
     except: 
      return queryset 

節はteacherprofilesとM2M分野を持っている、と私は基本的に結果がONLY教師が監督しているセクションが含まれていることを確認します。

この実装では、一致するすべてのクエリが返されますが、フィルタ条件は無視され、エラーは発生しません。

私が得た「最良の」結果は、ビューでfilter_querysetを混乱させ、クエリーセットに.filter(監督者=教師)を追加することでしたが、スーパーバイザのステータスに関係なく、クエリに一致するすべてのコースを追加します。

答えて

0

私は最終的にSearchQuerySetを使用してクラスの結果を返し、その結果をリストに変換し、教師なしのアイテムをスーパーバイザーとして削除しました。

これはおそらく最も効率的な方法ではありませんが、Haystackだけで動作させることはできませんでした(filter_and(name、supervisor)条件でSQSを使用しても)。しかし動作し、賢い。

関連する問題