Built-in Field Validations in Django models are the validations that come predefined to all Django fields. Every field comes in with built-in validations from Django validators. One can also add more built-in field validations for applying or removing certain constraints on a particular field.help_text
attribute is used to display the “help” text along with the field in form in admin interface or ModelForm. It’s useful for documentation even if your field isn’t used on a form. For example, you can define the pattern of date to be taken as input in the help_text
of DateField.
Syntax-
field_name = models.Field(help_text = "text")
Django Built-in Field Validation help_text
Explanation
Illustration of help_text using an Example. Consider a project named neveropen
having an app named Lazyroar
.
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into models.py
file of Lazyroar app. We will be using DateField for experimenting with help_text.
from django.db import models from django.db.models import Model # Create your models here. class GeeksModel(Model): Lazyroar_field = models.DateField( help_text = "Please use the following format: <em>YYYY-MM-DD</em>." ) |
After running makemigrations and migrate on Django and rendering the above model, let us check if something has happened to our field in Django admin Interface.
You can see extra text added at the bottom of the field. This was you can modify the text to be displayed below your field in ModelForm.
Advanced Concepts with help_text
–
=> What do I do, help_text is not being displayed even after a lot of tries?
Putting {{ form.as_p }}
(or just {{ form }}
) in your template should display the help_text without additional code, provided that you have form in your context or if you are using individual fields you can use {{ form.field.help_text }}
to access the help text of particular field.
More Built-in Field Validations
Field Options | Description |
---|---|
Null | If True, Django will store empty values as NULL in the database. Default is False. |
Blank | If True, the field is allowed to be blank. Default is False. |
db_column | The name of the database column to use for this field. If this isn’t given, Django will use the field’s name. |
Default | The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created. |
help_text | Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. |
primary_key | If True, this field is the primary key for the model. |
editable | If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True. |
error_messages | The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override. |
help_text | Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. |
verbose_name | A human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces. |
validators | A list of validators to run for this field. See the validators documentation for more information. |
Unique | If True, this field must be unique throughout the table. |