[Answered ]-Django admin:custom search

2👍

you are should overwriteget_search_results method in your ModelAdmin class as below:

class X_Admin_Form(admin.ModelAdmin):
    def get_search_results(self, request, queryset, search_term):   # for customize search_list
        queryset,use_distinct = super(X_Admin_Form, self).get_search_results(request,queryset,search_term)
        try:
            b = Site_Posts.objects.filter(title__icontains=search_term)
            ll = []
            for item in b:
               ll.append(item.id)
            condition = Q(post_id__in=ll) | Q(another_field=search_term)
            queryset |= self.model.objects.filter(condition)

        except:
            pass

        return queryset, use_distinct
👤masood

0👍

The search fields should be a list, not a tuple.

class Comment_Admin_Form(admin.ModelAdmin):
    model = Comment
    search_fields = ['content','post_title__title']

Leave a comment