About Lesson
Using ModelForm
for Automatic Form Generation
Model forms automatically generate the fields based on the model’s structure. You don’t need to define each field manually.
Model:
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
published_date = models.DateField()
ModelForm:
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['title', 'author', 'published_date']
This form will automatically create fields for title
, author
, and published_date
based on the Book
model.
Join the conversation