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:
However, it has the following limitations:
- The search box’s help text, completely ignores the newline escape character.
- 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.