Day 33 of 100 Days of Django: Model Form in Django.

Aman Khan
2 min readAug 23, 2022

--

Hi, Dev’s👏, Let’s see What is Model Form in Django?

Django provides a helper class that lets you create a Form class from a django model. This helper is called a Model Form.

Model Form is a regular form which can automatically generate certain fields.

The field that are automatically generated depend on the content of the Meta class and on which fields have already been defined declaratively.

Steps:

  1. Create Model Class
  2. Create Model Form Class

Syntax:

class ModelFormClassName(forms.ModelForm):
class Meta;
model = ModelClassName
fields = ["field1","field2"]

Notes:

If the model field has blank = True then required is set to false on the form filed. Otherwise required = True.

The form fields label is set to the verbose_name of the model field with the first character capitalized.

The form field help_text is set to the help_text of the model field.

If the model field has choice set then the form field widget be set to Select with choices coming from the model field choices. The choices will normally include the blank choice which is selected by default.

In the Next Article we are going to see Dynamic URL😁.

So Stay Tuned, I will see you in the next one.

Thanks for Following and Claps😋

Link to Starter Page

--

--

Aman Khan

Blockchain and Web3 Engineer Crafting compelling stories at the intersection of tech, business, and culture.