28👍
I got it working with following code.
class _GroupMessageForm(ModelForm):
class Meta:
model = GroupMessage
class GroupMessageForm(_GroupMessageForm):
def __init__(self, *args, **kwargs):
super(_GroupMessageForm, self).__init__(*args, **kwargs)
self.fields['employees'].help_text = '<br/>Hold down "Control" to select more.'
73👍
For Django >= 1.6 (docs):
class GroupForm(ModelForm):
class Meta:
model = GroupMessage
help_texts = {
'group': 'Group to which this message belongs to',
}
- [Django]-Django-AttributeError 'User' object has no attribute 'backend' (But….it does?)
- [Django]-Query datetime by today's date in Django
- [Django]-Reference list item by index within Django template?
8👍
There are multiple ways to do it.
1) You can add help_text to definition of the field in the Model, as Bobort said in the comment above:
class GroupMessage(Message):
group = models.ForeignKey(Group, related_name='+', help_text='Some text')
This way is useful if you want this help_text stay the same, no matter what ModelForm you use. For example you can create two ModelForm forms for GroupMessage model and they both will have help_text from this model.
2) You can override model field in form field like that:
class GroupForm(ModelForm):
group = forms.ModelChoseField(label='Group', help_text='Some text')
class Meta:
model = GroupMessage
Such way is useful when you need to change not only help_text but for example label or queryset or type of field.
3) You can do it as laffuste above:
class GroupForm(ModelForm):
class Meta:
model = GroupMessage
help_texts = {
'group': 'Group to which this message belongs to',
}
This way is useful if you only want to change help_text for one or more fields.
4) And another way to do it is something like you did:
class GroupForm(ModelForm):
class Meta:
model = GroupMessage
def __init__(self, *args, **kwargs):
super(GroupForm, self).__init__(*args, **kwargs)
self.fields['employees'].help_text = 'Some text'
But this solution needs small clarification. If you will use this form in template like that:
{% for field in form %}
{{ field }}
{{ field.help_text }}
{% endfor %}
It’s fine. But for example in case:
{% for field in form.visible_fields %}
{{ field }}
{{ field.help_text }}
{% endfor %}
help_text will be empty, because help_text in BoundField was filled before you set it by yourself. So solution will be to add self['employees'].help_text = 'Some text'
to __init__
or to use {{ field.field.help_text }}
in template.
This solution is useful in cases when you want to set help_text by some conditions, for example if specific argument was passed to form initialization or something else.
Hope it will help someone.
- [Django]-How to show related objects in Django/Admin?
- [Django]-How would I package and sell a Django app?
- [Django]-Django templates folders