[ACCEPTED]-How do I override help_text in django's admin interface-django-admin
You can create a new model form and override 4 the help_text there:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['myfield'].help_text = 'New help text!'
then use the new form 3 in your ModelAdmin:
class MyModel(admin.ModelAdmin):
...
form = MyForm
This is the cleaner way 2 to achieve what you want since form fields 1 belong to forms anyway!
In Django 1.9, similar to below works for 1 me
def get_form(self, request, obj=None, **kwargs):
form = super(MyAdmin, self).get_form(request, obj, **kwargs)
form.base_fields['my_field'].help_text = """
Some helpful text
"""
return form
An alternative way is to pass help_texts
keyword to 6 the get_form
method like so:
def get_form(self, *args, **kwargs):
help_texts = {'my_field': 'Field explanation'}
kwargs.update({'help_texts': help_texts})
return super().get_form(*args, **kwargs)
The help_texts
keyword gets eventually 5 passed to the modelform_factory
method and rendered as the 4 standard help text from a model in the Django 3 admin.
In case you're using an InlineModelAdmin
, you need 2 to override get_formset
in the same manner.
This also 1 works if you have readonly_fields
in your ModelAdmin subclass.
Cerin is right, but his code does not work 3 well (at least with Django 1.4).
def get_readonly_fields(self, request, obj):
try:
field = [f for f in obj._meta.fields if f.name == 'author']
if len(field) > 0:
field = field[0]
field.help_text = 'some special help text'
except:
pass
return self.readonly_fields
You will 2 have to change "author" and the help_text
string 1 to fit your needs.
You can always change form field attributes 1 on ModelAdmin constructor, something like:
def __init__(self, *args, **kwargs): super(ClassName, self).__init__(*args, **kwargs) if siteA: help_text = "foo" else: help_text = "bar" self.form.fields["field_name"].help_text = help_text
If you've defined a custom field in your 5 admin.py
only, and the field is not in your model, then 4 you can add help_text
using the class Meta
of a custom ModelForm
:
(For 3 example: you want to add a users photo on 2 your form, you can define a custom html 1 img
tag like this).
class SomeModelForm(forms.ModelForm):
# You don't need to define a custom form field or setup __init__()
class Meta:
model = SomeModel
help_texts = {"avatar": "User's avatar Image"}
# And tell your admin.py to use the ModelForm:
class SomeModelAdmin(admin.ModelAdmin):
form = SomeModelForm
# ... rest of the code
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.