2017-09-30 14 views
1

_unaccent_searchをDjangoフィルタで一緒に使用する方法を見つけましたが、管理ページの検索バーに正しく実装する方法を見つけようとしています。これまでのところ、私は以下のコードを持っていますが、正しく行っていないので、search_fields = []のような管理者設定は正しく適用されていません。元のDjangoの実装と私のソリューションをマージするのに役立つ人がいれば、私はそれを感謝します。上書きされ管理者のDjango _unaccentと_search

admin.ModelAdmin機能:

def get_search_results(self, request, queryset, search_term): 
    """ 
    Returns a tuple containing a queryset to implement the search, 
    and a boolean indicating if the results may contain duplicates. 
    """ 
    # Apply keyword searches. 
    def construct_search(field_name): 
     if field_name.startswith('^'): 
      return "%s__istartswith" % field_name[1:] 
     elif field_name.startswith('='): 
      return "%s__iexact" % field_name[1:] 
     elif field_name.startswith('@'): 
      return "%s__search" % field_name[1:] 
     else: 
      return "%s__icontains" % field_name 

    use_distinct = False 
    search_fields = self.get_search_fields(request) 
    if search_fields and search_term: 
     orm_lookups = [construct_search(str(search_field)) 
         for search_field in search_fields] 
     for bit in search_term.split(): 
      or_queries = [models.Q(**{orm_lookup: bit}) 
          for orm_lookup in orm_lookups] 
      queryset = queryset.filter(reduce(operator.or_, or_queries)) 
     if not use_distinct: 
      for search_spec in orm_lookups: 
       if lookup_needs_distinct(self.opts, search_spec): 
        use_distinct = True 
        break 

    return queryset, use_distinct 

マイコード:

def get_search_results(self, request, queryset, search_term): # TODO: Make this more professionally implemented (proper overrides) 
    """ Overrides default search completely to incorporate __search and __unaccent lookups """ 
    use_distinct = False 

    if search_term: # Note: "if" condition necessary to show ALL results in admin if not search_term is specified (otherwise shows 0 results) 
     queryset = queryset.annotate(unaccent_title=SearchVector('title', config='english_unaccent')).filter(unaccent_title=SearchQuery(search_term, config='english_unaccent')) 

    return queryset, use_distinct 

ジャンゴ:1.11.5

のPython:3.6.2

答えて

1

私があなたの質問から理解しているのは、search_fieldsと_unaccent searchの両方が一緒に働くことです。

質問が正しく理解されているかどうかはわかりません。あなたが何かを望んでいた場合

def get_search_results(self, request, queryset, search_term): # TODO: Make this more professionally implemented (proper overrides) 
    """ Overrides default search completely to incorporate __search and __unaccent lookups """ 
    queryset1, use_distinct = super(<Admin class>, self).get_search_results(request, queryset, search_term) 

    queryset2 = queryset 
    if search_term: # Note: "if" condition necessary to show ALL results in admin if not search_term is specified (otherwise shows 0 results) 
     queryset2 = queryset.annotate(unaccent_title=SearchVector(*self.search_fields, config='english_unaccent')).filter(unaccent_title=SearchQuery(search_term, config='english_unaccent')) 

    return queryset1 | queryset2, use_distinct 

は、私に教えてください:ここで

は、私が理解し何のためのソリューションです。

+0

完璧に作業しました - ありがとう! – Hybrid