[Fixed]-Add help_text for search field in admin.py

10👍

You can add this javascript in ModelAdmin

window.onload = function() {
    document.getElementById("searchbar").placeholder = "search with ";
};

ModelAdmin will be like

class SomeModelAdmin(admin.ModelAdmin):

    class Media:
        js = ('js/admin/custom_admin.js',)
        # css = { 'all': ('css/admin/custom_admin.css',)}

add the custom_admin.js in static/js/admin directory.

👤Javed

2👍

You could either override Admin template admin/search_form.html to add help text;
Or load a javascript file, which could find the dom node to insert the help text, in ProfileAdmin.Media, check the doc.

👤okm

1👍

The easiest solution is with jquery:

$(“#searchfield_id”).attr(‘title’, “Click here to search for someone”)

Or you can add it directly to the HTML itself. The title shows up when you mouse over the search field.

1👍

since django v 4.0 you can use ModelAdmin.search_help_text

from django.contrib import admin

class ProfileAdmin(admin.ModelAdmin):

    list_display = ('First_Name','Last_Name','Registeration_No','University','Batch','Sex')

    search_fields = ('First_Name','Last_Name','Registeration_No','University','Batch')

    search_help_text = f'search in: {", ".join(search_fields)}'

0👍

I wrote a decorator and use it

def add_search_help_text(get_changelist_instance):
    """
    Decorator that adds help_text for the admin search field
    
     Usage:
     In the child class admin.ModelAdmin add the attribute search_help_text = 'Your help text',
     and also add this get_changelist_instance function decorator:
         @add_search_help_text
         def get_changelist_instance(self, request):
             return super().get_changelist_instance(request)
     This decorator is relevant for Django version < 4.0
     In addition to the decorator to display help_text, you should override the admin/search_form.html template,
     by adding a container to the form:
      <div style="margin-left: 20px; width: 300px; font-size: small">{{ cl.search_help_text }}</div>
    """
    def wrapper(obj, request):
        if django.get_version() >= '4.0':
            import logging
            logging.warning(
                'Attribute "search_help_text" has been added to the main Django code. '
                 'Remove the "@add_search_help_text" decorator of the "get_changelist_instance" function '
                 f'class {obj.__class__.__name__} and also remove the template static/admin/search_form.html'
            )
        changelist = get_changelist_instance(obj, request)
        changelist.search_help_text = obj.search_help_text
        return changelist
    return wrapper

Another option is to create a mixin::

class SearchHelpTextMixin(admin.ModelAdmin):
    """
    A mixin that adds help_text for the admin search field

     Usage:
     In the child class admin.ModelAdmin add the attribute search_help_text = 'Your help text',
     and also add this mixin to the list of parent classes:

         class YourClassAdmin(SearchHelpTextMixin, admin.ModelAdmin):
             ...
             search_help_text = 'Your help text'

     This mixin is relevant for Django version < 4.0
     In addition to the mixin itself, to display help_text, you should override the admin/search_form.html template,
     by adding a container to the form:
     <div style="margin-left: 20px; width: 300px; font-size: small">{{ cl.search_help_text }}</div>
    """
    search_help_text = ''

    def get_changelist_instance(self, request):
        if django.get_version() >= '4.0':
            import logging
            logging.warning(
                'The "search_help_text" attribute has been added to the main Django code. '
                 f'Remove the class {self.__class__.__name__} from the mixin "{SearchHelpTextMixin.__name__}", '
                 f'and also remove the template static/admin/search_form.html'
            )
        changelist = super().get_changelist_instance(request)
        changelist.search_help_text = self.search_help_text
        return changelist

-1👍

Answer just for myself!

  1. Add an attr named search_fields_hint to my ModelAdmin subclass.
  2. Edit admin.templates.admin.search_form.html
  3. Add a attr placeholder="{{ cl.search_fields_hint }} to the <input>.
  4. Edit admin.views.main.py, ChangeList class
  5. Add self.search_fields_hint=search_fields_hint to the __init__.
  6. Edit admin.options.py, about line 1468, add parameter self.search_fields_hint to ChangList.

Leave a comment