Django管理员不会在changelist网址中显示搜索字词

时间:2016-09-28 22:38:43

标签: python django django-admin

我修改了django admin changelist的get_search_results方法以使用自定义搜索方法。我使用大文本字符串作为搜索的输入,所以我不希望搜索词出现在网址中。如何从更改列表网址中删除搜索字词?

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
    if search_term:
        # here input is large text string
        sites = get_sites_from_text(search_term)
        search_fields = self.get_search_fields(request)
        if len(sites) > 0:
            queryset = queryset.filter(site_name__in=sites)
        elif search_fields:
            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

0 个答案:

没有答案