17π
You can create a new model form and override 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 in your ModelAdmin:
class MyModel(admin.ModelAdmin):
...
form = MyForm
This is the cleaner way to achieve what you want since form fields belong to forms anyway!
10π
An alternative way is to pass help_texts
keyword to 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 passed to the modelform_factory
method and rendered as the standard help text from a model in the Django admin.
In case youβre using an InlineModelAdmin
, you need to override get_formset
in the same manner.
This also works if you have readonly_fields
in your ModelAdmin subclass.
- Display foreign key columns as link to detail object in Django admin
- Django, division between two annotate result won't calculate correctly
- Pass list of fields to QuerySet.values( )
- Error loading psycopg2 module: Library not loaded: libpq.5.dylib
8π
In Django 1.9, similar to below works for 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
- How do I check the content of a Django cache with Python memcached?
- Building wheel for cffi (setup.py) β¦ error while installing the packages from requirements.txt in django
- Django template rows of multiple items
- Building a Mobile App With jQuery Mobile, Django, and Phonegap
- How to use Pagination in a Non-Generic View/Viewset?
1π
Cerin is right, but his code does not work 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 have to change βauthorβ and the help_text
string to fit your needs.
- Django: Best Way to Add Javascript to Custom Widgets
- Many-to-many items in a template: check if any are not empty or none
- Django makemigrations not detecting project/apps/myapp
0π
You can always change form field attributes 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
- In Django how do I notify a parent when a child is saved in a foreign key relationship?
- Does a library to prevent duplicate form submissions exist for django?
- Error loading MySQLdb module: libmysqlclient.so.20: cannot open shared object file: No such file or directory
- 'CheckoutView' object has no attribute 'object'
- Celery β minimize memory consumption
0π
If youβve defined a custom field in your admin.py
only, and the field is not in your model, then you can add help_text
using the class Meta
of a custom ModelForm
:
(For example: you want to add a users photo on your form, you can define a custom html 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
- Django or similar for composite primary keys
- Uploading Large files to AWS S3 Bucket with Django on Heroku without 30s request timeout
- Django .aggregate() on .annotate()
- Datetime Field Received a Naive Datetime
-1π
Try this one (might need to replace self.fields with self.form.fields β¦)
class PropertyForm(models.ModelAdmin):
class Meta:
model = Property
def __init__(self, *args, **kwargs):
super(PropertyForm, self).__init__(*args, **kwargs)
for (key, val) in self.fields.iteritems():
self.fields[key].help_text = 'what_u_want'