[Django]-Can i add help text in django model fields

59👍

✅

Yes you can! Just like your form, you can add help_text to your model fields.

9👍

When using model forms you can add labels and help_texts to fields generated by the model. see docs

class PupilForm(forms.ModelForm):

  class Meta:
    model = Pupil

    fields = ['student_number',]
    labels = {'student_number': "Student Number",}
    help_texts = {'student_number': "Unique identifier for the student",}

6👍

After add help_texts in form, you should do smth like this in a frontend:

<label title="{{ form.name.help_text }}" for="{{ form.name.id_for_label }}">Your label</label> 
{{ form.name }} {{ form.quantity }}

4👍

By ‘detail page’ you mean a edit form of a single student instance or the list of all student records?
Are you using Django admin or do you use your own view and template, custom form definition or as_ul()/as_list() etc?
It’s hard to answer your question with just seeing your form field definition.

What do you mean by ‘for each table’?
Would form inheritance help, so that you set the help text of common form fields only in the super form.

If you render a custom template you can render the help_text wherever you like with
{{ my_field.help_text }}. If you have a table-like view in your template and want the helptext there, just put an empty instance of the form in your template context so that you have access to the help_texts and put it in your table as tooltip?

1👍

In case you want to use the standard admin change form with a short help-text for each field but sometimes feel the need to give a longer explanation or even a table with some sample values (without restricting the user to a predefined set of choices) you can do something like this:

my_strange_field = models.FloatField('strange parameter', validators=[MinValueValidator(1.0)],
                                     help_text='corr. factor x >= 1 <img src="/static/admin/img/icon-unknown.gif" '
                                               'width="15" height="15" title="typical values:\n'
                                               'cow:\t2..3\ncat:\t5..7\ndog:\t11..15')

This way you get a short text “corr. factor x >= 1” followed by a nifty question mark which presents a table like tool-tip without the need to modify the change_form template.

Leave a comment