代码之家  ›  专栏  ›  技术社区  ›  Hybrid

Django _uncent和_在Admin中搜索

  •  1
  • Hybrid  · 技术社区  · 7 年前

    所以我找到了一种使用 _unaccent _search search_fields = [] 未正确应用。如果有人能帮助我将我的解决方案与最初的Django实现合并,我将不胜感激。

    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
    

    Django:1.11.5

    1 回复  |  直到 7 年前
        1
  •  1
  •   Mohammad Mustaqeem    7 年前

    我从您的问题中了解到的是,您希望search\u字段和Uncent 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
    

    如果你想要别的东西,请告诉我。