Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a help_text to a ModelForm?

I know how to add a 'class' or other widget attribute to an automatically built ModelForm:

class ExampleSettingForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(ExampleSettingForm, self).__init__(*args, **kwargs)
        self.fields['example_field'].widget.attrs['class'] = 'css_class' 
    class Meta:
        model = Example

How do I insert a help_text= into the example_field Field?

like image 314
Bryce Avatar asked May 02 '14 01:05

Bryce


1 Answers

As of Django 1.6: You can edit it within the Meta class. Try:

class ExampleSettingForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(ExampleSettingForm, self).__init__(*args, **kwargs)
        self.fields['example_field'].widget.attrs['class'] = 'css_class' 
    class Meta:
        model = Example
        help_texts = {
                'example_field': ('Here is some help'),
        }

Docs on this are at https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-fields. See release notes at http://django.readthedocs.org/en/latest/releases/1.6.html . You can set your own label, help_text and error_messages.

like image 91
Alex Avatar answered Nov 15 '22 08:11

Alex