How to auto-populate django's admin.ModelAdmin.search_help_text with verbose field names of parameters defined in admin.ModelAdmin.search_fields

I understand that Django offers out-of-box customization of the django-admin’s changelist search functionality via its admin.ModelAdmin.search_fields and admin.ModelAdmin.search_help_text parameters.

I want to take this a bit further by having django auto-populate the admin.ModelAdmin.search_help_text parameter with the verbose_name of the fields specified in admin.ModelAdmin.search_fields.

For example, consider the following code:

class FinancialYearAdmin(admin.ModelAdmin):
    fields = ['financialYearEnding']
    list_display = ('financialYearEnding', 'dbEntryCreationDateTime', 'dbLastModifiedDateTime')
    search_fields = ['financialYearEnding']
    search_help_text = "Seach here using the following fields in the database:"
    for searchField in search_fields:
        search_help_text += FinancialYear._meta.get_field(searchField).verbose_name

It outputs the following search experience:

enter image description here

However, it has the following limitations:

  1. The search box’s help text, completely ignores the newline escape character.
  2. The last 3 lines have to be repeated in every ModelAdmin class that I create, albeit with change in the Model class name.

Is there a way whereby I could write those last 3 lines of code to either have the search_help_text auto-populated from the search_fields, or, have the search_help_text populated by making a function call without having to repeat the code across all child ModelAdmin classes?

Maybe through inheritance from an base abstract ModelAdmin class?

[Note:] financialYearEnding, dbEntryCreationDateTime and dbLastModifiedDateTime are all parameters in the FinancialYear Model class.

That’s how I would do it. (And have done it, for different reasons)

I’d create my own class, e.g. class MyAdmin(admin.ModelAdmin): and then inherit from it, e.g. class FinancialYearAdmin(MyAdmin)

I would suspect because it’s rendering this as HTML. If you want line breaks, you may need to replace the newlines with <br>.
(Note: I don’t see where any newline characters are involved with what you’ve posted here.)

Also, the ModelAdmin already has a get_search_fields method. That may be sufficient for what you’re trying to accomplish here.


and ‘\n’ does not work for new line in search_help_text.
I don’t know what works, if any at all.