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.
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
- Django: I get a [relation "auth_group" does not exist] error after syncdb
- Django – {% load url from future %} yields error "'url' is not a valid tag library"
- Using datetime to compare with dates in Django
- Save Django Foreign Key
- Following users like twitter in Django, how would you do it?
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.
- How to serialize groups of a user with Django-Rest-Framework
- Displaying both sides of a ManyToMany relationship in Django admin
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!
- Add an
attr
namedsearch_fields_hint
to myModelAdmin
subclass. - Edit admin.templates.admin.search_form.html
- Add a attr
placeholder="{{ cl.search_fields_hint }}
to the<input>
. - Edit admin.views.main.py, ChangeList class
- Add
self.search_fields_hint=search_fields_hint
to the__init__
. - Edit admin.options.py, about line 1468, add parameter
self.search_fields_hint
to ChangList.
- Display and format Django DurationField in template
- Django Model Field for Abstract Base Class
- How to get a model object using model name string in Django
- Show images in Django templates
- Django attribute error. 'module' object has no attribute 'rindex'
Source:stackexchange.com