Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override help_text in django's admin interface

I have multiple admin sites, so different users get a different experience of editing the objects in the database. Each admin site has a different set of objects exposed, and a different styling. All of this can be done by overriding templates and ModelAdmin objects.

I can't work out how to provide different help_text through the different sites. help_text is always taken straight from the model field definition, and there doesn't seem to be a way to override it.

Am I missing something, or is this impossible?

like image 617
Toby Avatar asked Apr 20 '11 14:04

Toby


1 Answers

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.

like image 131
Milan Cermak Avatar answered Oct 25 '22 16:10

Milan Cermak