17

I'm attempting to display an image when editing a user on the admin panel, but I can't figure out how to add help text.

I'm using this Django Admin Show Image from Imagefield code, which works fine.

However the short_description attribute only names the image, and help_text doesn't seem to add an text below it.

How can I add help_text to this field like normal model fields?

EDIT:

I would like to have help_text on the image like the password field does in this screenshot:

screenshot of admin page

3 Answers 3

16

Use a custom form if you don't want change a model:

from django import forms

class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['image'].help_text = 'My help text'

    class Meta:
        model = MyModel
        exclude = ()

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    form = MyForm
    # ...
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't seem to add the help text though. Am I missing something?
Does your field in the MyModel contain the help_text parameter like this: image = models.ImageField(<...>, help_text='My help text')?
I currently just pull the url from the model and I want to avoid adding anything else to it.
7

If you don't want to create a custom model form class :

class MyModelAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, change=False, **kwargs):
        form = super().get_form(request, obj=obj, change=change, **kwargs)
        form.base_fields["image"].help_text = "Some help text..."
        return form

Comments

4

It took me a while to figure out. If you've defined a custom field in your admin.py only and the field is not in your model. Use a custom ModelForm:

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"}
        exclude = ()

And in the admin.py:

class SomeModelAdmin(admin.ModelAdmin):
    form = SomeModelForm
    # ...

1 Comment

Thank you! I didn't have to define model or exclude attributes either (just help_texts) to make it work with the Admin panel :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.