开发者

How do I override help_text in django's admin interface

开发者 https://www.devze.com 2023-02-27 17:00 出处:网络
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

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?


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!


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


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.


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.


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


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


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' 
0

精彩评论

暂无评论...
验证码 换一张
取 消