Django admin search: how to override the default handler?

前端 未结 3 976
小鲜肉
小鲜肉 2021-02-14 08:00

I wish to customize the way in which search queries across the search_fields.

Is there a way to do it without hacking deeply into the Django code or creating a totally i

3条回答
  •  时光取名叫无心
    2021-02-14 09:03

    It is very easy to do this in django 1.6

    ModelAdmin.get_search_results(request, queryset, search_term) New in Django 1.6.

    import operator
    # from django.utils.six.moves import reduce  # if Python 3
    from django.db.models import Q
    
    class PersonAdmin(admin.ModelAdmin):
        list_display = ('name', 'age')
        search_fields = ('name',)
    
        def get_search_results(self, request, queryset, search_term):
            # search_term is what you input in admin site
            # queryset is search results
            queryset, use_distinct = super(PersonAdmin, self).get_search_results(request, queryset, search_term)
    
            search_term_list = search_term.split(' ')#['apple','bar']
            # you can also use `self.search_fields` instead of following `search_columns`
            search_columns = ('name','age','address')
            #convert to Q(name='apple') | Q(name='bar') | Q(age='apple') | ...
            query_condition = reduce(operator.or_, [Q(**{c:v}) for c in search_columns for v in search_term_list])
    
            queryset = self.model.objects.filter(query_condition)
            # NOTICE, if you want to use the query before
            # queryset = queryset.filter(query_condition)
            return queryset, use_distinct
    

提交回复
热议问题