[Fixed]-How do I override help_text in django's admin interface

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!

πŸ‘€ppetrid

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.

πŸ‘€Milan Cermak

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
πŸ‘€James Hiew

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.

πŸ‘€Steve K

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

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
πŸ‘€Nitin Nain

-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' 
πŸ‘€zzart

Leave a comment