我知道如何将'class'或其他widget属性添加到自动构建的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
如何在example_field Field
中插入 help_text = ?
答案 0 :(得分:8)
从Django 1.6开始:您可以在Meta
课程中进行编辑。尝试:
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'),
}
关于此的文档位于https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-fields。请参阅http://django.readthedocs.org/en/latest/releases/1.6.html上的发行说明。您可以设置自己的label
,help_text
和error_messages
。
答案 1 :(得分:2)
这就是我在Django 1.9中所做的:
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('__all__')
help_texts = {
"my_field": "This is case sensitive..."
}